Hi List,
I am hoping to get some advice on how to do this most efficiently.
I have a swing program wich has one JFrame "mainframe". I also have
several JPanels each with JButtons, text and images on them.
When the program starts I put a blank JPanel on the JFrame like this
mainframe.setContentPane(blankpanel);
As the program starts to run I need to switch out panels and display
other panels and animate some buttons,etc...
So this is the sequence I use for exchanging the JPanels and
redisplaying them.
mainframe.setContentPane(instpanel);
mainframe.show();
mainframe.repaint();
The Problem I have is that this is somewhat not reliable. Sometimes
the new JPanel does not get updated untill the user inteacts with the
new JPanel or the window gets resized.
Any advice on how to do this most efficiently or better will be greatly
appreciated.
Thank you
Rene
Lloyd - 22 Mar 2005 00:36 GMT
> As the program starts to run I need to switch out panels and display
> other panels and animate some buttons,etc...
[quoted text clipped - 5 lines]
> mainframe.show();
> mainframe.repaint();
Have a look at CardLayout for your panel.
John McGrath - 22 Mar 2005 01:28 GMT
> So this is the sequence I use for exchanging the JPanels and
> redisplaying them.
[quoted text clipped - 6 lines]
> the new JPanel does not get updated untill the user inteacts with the
> new JPanel or the window gets resized.
The problem is that the panels are not being validated, although I am not
sure why that is the case. My guess would be that the panels are not
being invalidated, so the show() call does not validate them.
I am not sure why the code is doing a show() on the frame. If the frame
is already showing, there is not much point to this, and it could have
some side effects that you do not want, such as bringing the frame in
front of other windows. Also, setVisible(true) should be used instead of
show(), since show() is deprecated as of Java 5.
Instead of switching out the panels, you should consider placing them in a
panel controlled by a CardLayout. Then, you just need to tell the layout
to switch the showing panel, and everything should work correctly. There
is one implication to this approach: the panels must be constructed ahead
of time. But that should not be a major consideration, unless you have a
large number of panels you want to show in the frame.

Signature
Regards,
John McGrath