aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Issues with hiding a panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Issues with hiding a panel" Watch "Issues with hiding a panel" New topic
Author

Issues with hiding a panel

James Filson
Greenhorn

Joined: Nov 24, 2009
Posts: 11
Hi,

It's my first post - so please be easy on me! My simple aim is to have my JPanel disappear (set to invisible) when a button is clicked. The below code does this perfectly, until I add in some code or a function to perform after it is hidden. When I do this, it seems the code below it is firstly run and then the panel is hidden. I can't understand why this is happening when I am asking to hide it before the extra code. I'm even putting the extra code in an if loop which shouldn't be touched (as far as I know) until it is hidden!



Any ideas? I'm totally stumped and any help would be greatly appreciated.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
it seems the code below it is firstly run and then the panel is hidden


That is correct. All the code executes on the EDT which means the GUI can't repaint itself until all the code is finished executing. You need to create a separate Thread for the long running code so you don't block the EDT.

For more information check out the Concurrency section from the Swing tutorial.
James Filson
Greenhorn

Joined: Nov 24, 2009
Posts: 11
Rob Camick wrote:
it seems the code below it is firstly run and then the panel is hidden


That is correct. All the code executes on the EDT which means the GUI can't repaint itself until all the code is finished executing. You need to create a separate Thread for the long running code so you don't block the EDT.

For more information check out the Concurrency section from the Swing tutorial.

Thanks so much for your quick reply. I have been reading articles on threads and concurrency for half an hour now and, being new to Java, I'm having trouble getting my head around this.

I currently have two functions, one to hide the main JLabel (which is layered over the top of the main JLabel) and another which paints some items to the main JLabel:

I was originally calling them like so:



From what I can ascertain, to use threads I need

around each function. I can then call each with:


Is this correct, or at least being close to be? I'm not getting anything as it is, and the lack of actual errors leaves me very stuck! Thanks again
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
The long running code that takes time needs to be in a separate Thread. The code that updates the GUI needs to execute on the EDT.

The tutorial shows an example of using a SwingWorker to do both parts of this.

The other approach is to do it yourself by creating a separate Thread for the long running task. Then within that code you use SwingUtilities.invokeLater() to add code to the EDT. Here is an old demo I have of the second approach:


James Filson
Greenhorn

Joined: Nov 24, 2009
Posts: 11
Wow, thank you very much Rob. Maybe I'm just being stupid but threads are never anything I've encountered before in programming. That example you gave helped a huge deal, and this is what I managed to produce (with irrelevant code stripped):

Above I have create three Runnables, defined threads to 'hold?' them in, and then called these threads in turn. It seems however that they are running at the same time, as thread3 will finish before thread2. I have stripped out all extraneous code (leaving just simple if statements and paint commands) inside these so I can't see much more I can do... How can I get each to run in turn, and not to run the next until it has completely finished drawing to the panels?

I can't thank you enough for your help so far,
James
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Issues with hiding a panel