JPanels one above the other

I want to design a jFrame where there are three jButtons and a set of three jpanels one above the other. When we call a jPanel from the respective jButton , that pane will be displayed. It would appear to the user as if the same portion of the jFrame is displaying the content to be shown on clicking each jButton.But when i am trying to set the jPanels one above the other, they are being shown side by side thus elongating the jFrame horizontally. What should i do to put one jPanel over the other? Any other idea than jPanel which should do the work i intend to do would also be help !!

Upvotes: 0

Views: 188

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208944

Your behavior sounds like you are using a FlowLayout. This will not "layer" anything. Instead us a CardLayout, which does exactly what you are trying to accomplish. You call method like show, next, and previous to navigate the panels. See How to Use CardLayout for more details.

Also there are probably hundreds of other examples here on so. Go through the questions. [Tip: navigate the different tabs like "votes" and "frequent" to filter to some of the better posts]

Here's one that uses the show() method to switch between two panels by name.

Upvotes: 2

Related Questions