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.

