Revalidate and repaint - Java Swing

Posted by bosra on Stack Overflow See other posts from Stack Overflow or by bosra
Published on 2012-12-09T16:34:05Z Indexed on 2012/12/09 17:03 UTC
Read the original article Hit count: 179

Filed under:
|
|

I have a JPanel that I am adding JLabel's to. I then want to remove all the JLabels and add some new ones.

So I do the following:

        panel.removeAll();panel.repaint();

        panel.add(new JLabel("Add something new");

        panel.revalidate(); 

This works fine. My problem arises when I start a new thread after this like:

    panel.removeAll();panel.repaint();

    (1)panel.add(new JLabel("Add something new");

    panel.revalidate();

    //new thread to start - this thread creates new JLabels that should appear under (1)
    firstProducer.start();              

    try {

            firstProducer.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Then the output from the original JLabels is still visible. I have read that the revalidate process is a long running task and hence the firstProducer thread is getting started while the revalidation is going on and a conflict is arising. What is the best way to deal with this?

© Stack Overflow or respective owner

Related posts about java

Related posts about swing