The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Funny business with JFrame, JPanel and JLabel

All, I'm having some funny business creating instances of a Swing GUI app.

My objective is to provide my user with a message while a process is busy doing some work. The controlling program is a created instance of a class ( Call it class1 ). This instance creates an instance of another class ( class2 ) where the constructor takes some time to do its work. While that constructor is busy, I want a message like "Listing contents of directory . . . . ", where the dots are slowly added as time goes by to show the user there is no lockup.

In class1 I create a frame, panel and label. I add label to panel to frame. I pass the label to a threaded instance of a method that adds the message text to the label and slowly adds the dots. While that thread is running I instantiate class2, unthreaded. When that is finished, I interrupt the messaging thread and all is well.

It works for the first instance of class 1. When I create a second instance of class1, the label does not show.

I assume there is something fundamental that I am missing here. It does exactly what I want for the first instance of class1, but in following instances it does not show the label.

The threaded method does run because I can see it in the standard output. It's just as if the label isn't there.

NOW: My first instance of class1 is created by the static main method. When I create multiple instances of class 1 in the main method, it works. So I created another method in the main class, static, to start another instance of class 1. It fails as well.

I'm sure someone knowledgeable can point me in the right direction.

I reduced my code to the bare minimum to illustrate the problem. SHould run fine.

Re: Funny business with JFrame, JPanel and JLabel

Originally Posted by JeffC

What a shame. Spam in the replies. Another internet resource corrupted.

Indeed. If I had the power I would

Back to the problem at hand, something funny is going on with your threading. At least in the case of your init to simulate initialization (waiting two seconds), you are essentially freezing the gui by putting the EDT to sleep. This does not happen initially because you are initializing on the main thread, and sleeping the main thread. However afterwards you create a new frame from the EDT. To demonstrate, change you main method to:

And this first frame will behave similar to the second in your example. As a side note, you should place all calls to Swing updates from other threads onto the EDT using SwingUtilities (unless noted as thread safe in the documentation)