Andrew
Andrew

Reputation: 175

Java - Using Netbeans to hide/show external jPanels?

I'm new to GUI developing with java and I'm using netbeans to help me design various jPanels. Now I have one class with my jFrame in it and I'll be putting a menu on the left with a jPanel on the right and when someone makes a selection on the left, I want to call the jPanel class and place it in that panel on the right. I've seen various different examples but I can't get it to work. Here's how my file structure works

Now, how do I put panel1 in my jframe when someone selects it in my menu?

Upvotes: 0

Views: 5683

Answers (3)

KSK
KSK

Reputation: 158

Totally agree with above. I have used CardLayout very successfully.

But don't forget to call revalidate on any component you change (whether you're using CardLayout or not). This tells the UI that it needs to update to the screen.

Upvotes: 0

MikeM
MikeM

Reputation: 342

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.

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 208994

You probably want to use a CardLayout that will let you switch between views(panels). You can see more at How to Use CardLayout. You can just call cardLayout.show(...) and the panel you want will appear (so to speak).

Also for the Netbean builder tool, you can see How to use CardLayout with Netbeans GUI Builder.

Also see this post. You can drag and drop your class panels into the design view

Upvotes: 1

Related Questions