Question

I have an array of Jlabels and each has an imageIcon attached to it. the attachment is happening outside the constructor in a method that is been called several times and at the end of the method i use repaint().

the problem is, that after a certin Jlabel has been draw, i can't make him to "unDraw, even if i change the Jlabel (or the image) to null. is seems to just stay there.

i tried to repaint again, to reset the background color and to revalidate, and non seems to do the job.

any ideas?

EDIT: i tried the following (inside the edt) and it seems to only change the last JLable on the array:

for (int j = 0;j<playerCards.length;j++){
    remove(playerCards[j]);
}
Was it helpful?

Solution

Try moving your UI operations into the EDT thread. Not making them in there can lead to unpredictable results.

EDT - Event Dispatch Thread

You should use the EDT thread to make changes to GUI so that this is the only thread that makes changes to it.

    SwingUtilities.invokeLater( 
        new Runnable() {
            public void run() {
                //UI changes here
            }
        }
    );

OTHER TIPS

Try using

SwingUtilities.updateComponentTreeUI(YOUR_OBJECT_CONTAINER);

where YOUR_OBJECT_CONTAINER is a java object that extends JComponent that contains the JLabel you want to remove.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top