LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Load different JPanel when different Menu Iteks are selected (http://www.linuxquestions.org/questions/programming-9/load-different-jpanel-when-different-menu-iteks-are-selected-471858/)

ppanyam 08-08-2006 03:43 AM

Load different JPanel when different Menu Iteks are selected
 
In a java application, I want to load a different JPanel with different widgets in BorderLayout.Centre of a JFrame.

However, if user selects a differnet Menu Item, the original JPanel has to be removed and in its place another JPanel with different widgets must be displayed and so on. In some cases, the JPanel may display a graphic or a text message only.

Where can I find a suitable article or link for the above exercise?

I am using java version 1.4.2 on Windows and IRIX.

Thanks in advance

ppanyam

anupamsr 08-08-2006 05:20 AM

If I am correct, you need to create a parent JPanel and create another JPanel in it. Then you can keep replacing this JPanel. Here is the sample code:
Code:

parentJPanel.removeAll();
parentJPanel.add(newJPanel);
parentJPanel.setVisible(false);
parentJPanel.setVisible(true);

Keep note of last two lines, without which your change will not be visible (repaint() somehow doesnt work).

ppanyam 08-08-2006 08:57 AM

Why are we using

Code:

parentJPanel.setVisible(false);
parentJPanel.setVisible(true);

one after the other?

anupamsr 08-08-2006 09:13 AM

yeah, because otherwise it wont refresh :) I found this on some Java forum once because my parentJPanel.repaint() was not giving the expected results. So you force it to repaint by doing the above one after the other.

xhi 08-08-2006 09:52 AM

Quote:

Originally Posted by anupamsr
yeah, because otherwise it wont refresh :) I found this on some Java forum once because my parentJPanel.repaint() was not giving the expected results. So you force it to repaint by doing the above one after the other.

thats weird.

the removeAll and add should perform refreshing shouldnt they?

anupamsr 08-09-2006 12:53 AM

Yeah, but as I said, it didn't. Then I searched on google and found it posted on some forum that if it doesn't, you can force it this way. So there I go :)

ppanyam 08-09-2006 03:15 AM

Thanks a lot. Let me try it.


All times are GMT -5. The time now is 08:05 PM.