views:

23

answers:

2

So, I have several JPanels that contain buttons, labels, etc. that I want to switch between from a main JFrame. Currently I am trying to use the this.add(JPanelname); method and this.remove(JPanelname); with the validate(); and repaint(); methods

The problem is it will add the panel to the JFrame but it will not remove it. I am not sure how exactly to go about this and any help would be greatly appreciated.

A: 

If you are constantly switching between JPanels, then a JTabbedPane may be the right thing to use. It should not be necessary to call "validate" or "repaint" when you add or remove a JPanel. Do you have a layout manager installed? Do you make sure to call add/remove only within the UI event thread? Also, typically one does not call "validate()" but rather "invalidate()" to invalidate the container for updates.

Michael Aaron Safyan
Thank you for the fast response! A JTabbedPane is not exactly the look I was going for, I should have described it a little more thoroughly. I have a main pane that works as a sort of desktop i guess you could call it where the buttons are like widgets that when clicked will load separate parts of the program(IE a setup screen) and then when the user is finished they can hit a back button and then the panel will close (possibly be removed from the frame and then the frame repainted?) and the main screen will be shown again. I may be going about this the wrong way hope this makes it more clear
jt153
+1  A: 

Maybe you should be using a Card Layout.

Or maybe you should be using modal JDialogs. So whenever you click on the "widjet" a new window is displayed. Then when you close the dialog you are back on your main frame.

camickr