0

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

  • connector.java (creates connection to db and calls jframe class)
  • jframe.java (contains menu on left and an empty jpanel on right)
  • panel1.java (panel class i designed in netbeans) panel2.java
  • panel3.java

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

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Andrew
  • 175
  • 1
  • 2
  • 12

3 Answers3

1

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

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • My one concern is that by using card layout I have to have everything in one java file, correct? I'm hesitant to do that because the file will quickly become huge given the scope of this project. What I'm doing is converting a text/terminal based program to a GUI program and I've never done something of this scale before. So in my mind keeping the 20+ panels in separate files seems more manageable to me then putting all of them in the same file. – Andrew Jul 18 '14 at 12:20
  • No, who says? You can easily drag and drop your panel classes onto the main panel (with the CardLayout). See last link. I don't see 20 panels being a problem – Paul Samsotha Jul 18 '14 at 12:22
  • See my first comment, I was making an edit. I'm new to stack overflow and didn't know the enter key submitted haha – Andrew Jul 18 '14 at 12:23
  • If you are using the builder tool, see the link above. Instead of adding panels from the palette, just add your panel classes, and give each panel a name. When the menu is pressed, show the corresponding panel – Paul Samsotha Jul 18 '14 at 12:26
  • Another possible design is to create a panel class (with a Cardlayout) to just hold all the other panel. Have a method in that class to `show(String panelName)`. This will make the main class cleaner – Paul Samsotha Jul 18 '14 at 12:36
0

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.

MikeM
  • 342
  • 2
  • 10
0

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.

KSK
  • 149
  • 3
  • 15