Okay so I have a screen with a toolbar on the top (BorderLayout.NORTH) with about 5 buttons. What I want to do is be able to switch back and forth between JPanels with each button click. so each time I click one of the buttons a new JPanel appears in BorderLayout.CENTER. Right now what I have is a JPanel inside BorderLayout.CENTER and each section (north south east west and center) holds a JPanel and when one button is pressed the visibility of all the others is turned to false and the one I want to look at is true. This seems like a very crude way to get at this problem but it is all I can think of right now. Any suggestions how I might do this better???

Oh also I would like to create a new JPanel that appears on the main screen and not simply just turn the visibility of an existing one on or off.

CardLayout

Na I think that will only keep the same JPanel open and not create a new object.

I think I figured it out though. I added a new JPanel in the center and then if you panel.remove(oldJPanel) and then panel.add(newJPanel) all you have to do is use a few commands:
repaint();
invalidate();
validate();

That seems to work for me right now so I guess I'll stick with that. Unless this is bad practice for some reason? If so anyone have any other ideas?

Wht do you think CardLayout does in the background? And without all the fuss and "erone-pronedness" of doing it yourself.

You simply add each Panel to the CardLayout then, when a button is pushed, you tell the CardLayout which of those is to be displayed. Where's the problem?

But, hey, if you want to reinvent the wheel, go right ahead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.