You could use a CardLayout
, or you could use some setVisible() calls. I'm not sure if you're asking how to set up the frame, but you could do that with a BoxLayout:
JFrame f = new JFrame();
JPanel wholePanel = new JPanel();
wholePanel.setLayout(new BoxLayout(wholePanel, BoxLayout.X_AXIS)); // panel with a vertical split, i.e. new panels get added as new "columns"
wholePanel.add(menuPanel);
JPanel potentialPanels = new JPanel(); // use this to act as a single panel on the right
potentialPanels.add(panel1); // it'll contain both panel1 and panel3, but only show one at a time
potentialPanels.add(panel3);
panel3.setVisible(false); // panel 3 invisible by default
showPanel1Button.addActionListener(new ActionListener(){ // activates upon selection
@Override
public void actionPerformed(ActionEvent e) {
panel1.setVisible(true); // show panel 1
panel3.setVisible(false); // /hide panel 3
}
});
showPanel3Button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
panel1.setVisible(false); // do the reverse: hide panel 1, show panel 3
panel3.setVisible(true);
}
});
An alternative would be to use a CardLayout for the potentialPanels
panel.
Based on your comment on the above post, if you're concerned about having a lot of panels, then you could use an ArrayList
to contain all of panels. Then you could set one of those panels as visible, temporarily remove it from the list, and iterate over the rest of the list setting them all as "invisible", and then add that visible panel to the list again. It'd be more efficient to use a CardLayout, where you show a single panel and the others are "hidden", in effectively the same way but with the built in show
function, avoiding the clutter of the ArrayList
.