Repainting and threading - the eternal problem...  
Author Message
Jonas Lindquist





PostPosted: 2008-6-17 21:34:00 Top

java-programmer, Repainting and threading - the eternal problem... Hi there folks!

I have a program, with MANY lines of code, so I will not post the
entire program here. However, this is my problem.

I have a GUI program based on swing which contains a JList:

===
this.userTrapList = new JList(this.userTrapListModel);

this.userTrapList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
this.userTrapList.setLayoutOrientation(JList.VERTICAL);
this.userTrapList.addMouseListener(this);
this.userTrapList.setCellRenderer(new MyCellRenderer());
===

As you can see, I also have a custom cell render called
MyCellRenderer:

===
private class MyCellRenderer extends DefaultListCellRenderer {

public static final long serialVersionUID = 1L;

public Component getListCellRendererComponent(JList list, Object
value,
int index, boolean isSelected, boolean cellHasFocus) {

JLabel label = (JLabel) super.getListCellRendererComponent(list,
value, index, isSelected, cellHasFocus);

TSListObject tsl = null;
try {
tsl = (TSListObject)value;
} catch (ClassCastException cce) { }

if (tsl != null && tsl.hasBeenSent()) {
// if tsl has been sent, set background.
label.setBackground(hasBeenSentColor);
}

//// HERE I LEFT SOME CODE OUT ////

return label;
}
}
===

One of the features of the cell renderer is that, once a TSListObject
(which is what the JList contains) flag "hasBeenSend" is true, the
background color is set to green.

What the program does it iterates over the items of the list and sends
them. The class which sends them implements runnable, and here is how
I send them:

===
Enumeration<TSListObject> en = this.userTrapListModel.elements();
while (en.hasMoreElements()) {

TSListObject tmpTSL = en.nextElement();

/* THIS IS WHERE THE SENDING HAPPENS! */

SnmpPDU pdu = tmpTSL.getSnmpPDU();

TrapSenderModel ts = new TrapSenderModel(pdu, session,
dstHost.getText(), spinnerInt.intValue(), new Debug(1));

ts.addObserver(this);
Thread tsThread = new Thread(ts);
tsl.setHasBeenSent(true);
tsThread.start();

try {
tsThread.join();
Thread.sleep(500);
} catch(InterruptedException ie) {
//Catch nothing
}
}
===

As you can see, the actual sending happens when TrapSenderModel's
method run() is executed, which is the same as tsThread.start();

The TrapSenderModel extends Observable and every time it does
send(pdu) (which is not displayed here), it setChanged() and
notifyObservers(pdu).

In my GUI program, which implements Observer, I have an update()
method:

===
public void update(Observable obs, Object obj) {
System.out.println("Update caught!");
this.userTrapList.repaint();
}
===


Okey... Now that we've got the background covered, lets get down to
the problem:

If my JList contains 9 items, and I send them all, the output to
System.out is a nice print of "Update Caught" every 500 milliseconds,
HOWEVER, the background color of the labels do not update until all
items have been sent.

I would like the background of each cell to change momentaneously, as,
if we have a 0.5 second wait between sending every item, and we send
100 items, that means 50 seconds of not showing any change. This will
probably make the user impatient or worried.

I haev tried several methods to get this working, however, I did never
become best friends with SwingWorker and such....


I'm just thinking that, since the sending happens in a thread of its
own, why cannot the GUI update in its own native thread?

What have I missed? I've spent the entire day here and havn't come up
with anything!


I'm greatful for any help I can get (and yes, I've read manuals,
articles, forums, google and so on).

Yours,
Jonas
 
Roedy Green





PostPosted: 2008-6-18 1:20:00 Top

java-programmer >> Repainting and threading - the eternal problem... On Tue, 17 Jun 2008 06:33:44 -0700 (PDT), Jonas Lindquist
<email***@***.com> wrote, quoted or indirectly quoted someone
who said :

>tsThread.join();
>Thread.sleep(500);

This sort of code almost never works unless your name is Doug Lea.

Have a look at the higher level tools described in:
http://mindprod.com/jgloss/threadsafe.html
http://mindprod.com/jgloss/timer.html
http://mindprod.com/jgloss/thread.html
http://mindprod.com/jgloss/queue.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Jonas Lindquist





PostPosted: 2008-6-18 18:25:00 Top

java-programmer >> Repainting and threading - the eternal problem... Problem solved. Thankyou.


SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
public Void doInBackground() {

for (JComponent c: guiComponents)
c.setEnabled(!c.isEnabled());

Enumeration<?> en = userTrapListModel.elements();
while (en.hasMoreElements()) {
Object o = en.nextElement();
if (o instanceof TSListObject) {
TSListObject tmpTSL = (TSListObject)o;
if (tmpTSL.hasBeenSent()) {
clearAllHasBeenSentFlags();
}

/* THIS IS WHERE THE SENDING HAPPENS! */
send(tmpTSL);

if (tmpTSL.isPauseSendAfter()) {
int answer = JOptionPane.showConfirmDialog(null, tmpTSL.getName()
+" has been sent. Would you like to continue now?", tmpTSL.getName(),
JOptionPane.YES_NO_OPTION);
if (answer != JOptionPane.YES_OPTION) {
break;
}
}
}
}
JOptionPane.showMessageDialog(null, "All traps have been sent!");
for (JComponent c: guiComponents)
c.setEnabled(!c.isEnabled());
return null;
}
};
worker.execute();
 
 
Roedy Green





PostPosted: 2008-6-19 4:49:00 Top

java-programmer >> Repainting and threading - the eternal problem... On Tue, 17 Jun 2008 17:19:46 GMT, Roedy Green
<email***@***.com> wrote, quoted or indirectly quoted
someone who said :

>Have a look at the higher level tools described in:
>http://mindprod.com/jgloss/threadsafe.html
>http://mindprod.com/jgloss/timer.html
>http://mindprod.com/jgloss/thread.html
>http://mindprod.com/jgloss/queue.html

and one more
http://mindprod.com/jgloss/swingworker.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
 
Jack





PostPosted: 2008-6-19 21:00:00 Top

java-programmer >> Repainting and threading - the eternal problem... Roedy Green a 閏rit :
> On Tue, 17 Jun 2008 17:19:46 GMT, Roedy Green
> <email***@***.com> wrote, quoted or indirectly quoted
> someone who said :
>
>> Have a look at the higher level tools described in:
>> http://mindprod.com/jgloss/threadsafe.html
>> http://mindprod.com/jgloss/timer.html
>> http://mindprod.com/jgloss/thread.html
>> http://mindprod.com/jgloss/queue.html
>
> and one more
> http://mindprod.com/jgloss/swingworker.html

You must spend so much time updating your website!