My JPanel is repainting

Hi everyone,

I'm new at this, so please tell me if I'm providing too little information, too much or in the wrong format. Any help would be appreciated. My problem:

I've extended JPanel to create "SimPanel", which is a GridLayout with Labels in it:

public SimPanel(Simulation sim){

this.setPreferredSize(new Dimension(panelSize, panelSize));

this.labels =new JLabel[simSize][simSize];

setLayout(new GridLayout(simSize, simSize));

for(int y = 0; y < simSize; y++){

for(int x = 0; x < simSize; x++){

labels[x][y] =new JLabel("");

labels[x][y].setOpaque(true);

labels[x][y].setBorder(BorderFactory.createLineBorder(Color.black));

labels[x][y].setHorizontalAlignment(SwingConstants.CENTER);

labels[x][y].setVerticalAlignment(SwingConstants.CENTER);

this.add(labels[x][y]);

}

}

}

I've written a function "paintLabel" which (simplified) does this:

publicvoid paintLabel(int x,int y){

int x = tile.getXPosition();

int y = tile.getYPosition();

JLabel label = labels[x][y];

if ('X'){// where X is some real condition, elsewhere in the programme

label.setText("X");

}

else{

label.setText("Y");

}

}

The point is to be able to repaint a specific label when required. This works fine. One call to simPanel.repaintLabel() does exactly what I want it to, repainting the specified label and nothing else. But if I do this:

simPanel.repaintLabel(0,0);

simPanel.repaintLabel(1,0);

...I really want to see (0,0) repaint FIRST and (1,1) repaint second. But no matter what I do, the two paintcommands are always done together. I've tried this:

simPanel.repaintLabel(0,0);

try{ Thread.currentThread().sleep(1000);}catch (Exception e){}

simPanel.repaintLabel(1,0);

...but then it just waits a second, before repainting both label (0,0) and label (1,0) at once. How in the world do I get it to repaint twice, and showing each repaint separately? I'm really at my wit's end here.

Thanks,

Elske.

[3349 byte] By [elskevdva] at [2007-10-2 17:11:49]
# 1

> How in the world do I get it to repaint twice, and showing each repaint separately?

It sounds like your code is executing in the Event Thread, which means the Thread.sleep(...) command is causing the GUI to sleep before the component gets repainted. So you need to create a separate Thread and invoke your repaint and sleep commands from that thread. That way the newly created Thread, not the GUI Event Thread, sleeps. Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html]How to Use Threads[/url].

Or maybe you could use a Timer to fire an event to repaint each label. The tutorial also has a secton on "How to Use Timers".

camickra at 2007-7-13 18:26:59 > top of Java-index,Desktop,Core GUI APIs...
# 2

Note: The painting subsystem doesn't necessarily repaint objects immediately (which is why the Thread.sleep() causes a problem). It will try to merge multiple repaint requests into a single request (to be more efficient).

So, another option would be to force the repainting of each component using the paintImmediately(...) method.

Although in the long run it is better for you to understand how Threads work.

camickra at 2007-7-13 18:26:59 > top of Java-index,Desktop,Core GUI APIs...
# 3

Hi. Thanks for the advice. I have been trying to understand how Threads work, but the little understanding I have gained so far is not helping me solve the problem. I'm also finding it a little ironic that I have to get into multithreading just to understand how to make my program run <i> sequentially </i>.

I think I've tried both of your suggestions before, but perhaps not implemented either quite right. More generally, my act-repaint cycle is within a for-loop, so you get this:

for (....) {

change state

repaint()

}

Then you can use synchronized and a boolean flag to make sure it doesn't repaint until the state has changed, and doesn't change the state until repaint is done, but you get the problem that the for-loop is so much faster than the repainting, that the flag for "change state" is never true, and it runs through the for-loop without ever changing the state. Arggh.

But I can try using your suggestion for a new thread in a slightly different way than I have before. Maybe that will work. Thanks.

elskevdva at 2007-7-13 18:26:59 > top of Java-index,Desktop,Core GUI APIs...