0

I am planning to make a program that in the top of the contentPane has a menubar.

Under this menubar another JPanel, here is what I did (it works), but I don't know if this is the best way:

I made a lot of JPanels with different buttons, I want that a JMenuItem changes the screen(JPanel)

So what I did for each JMenuItem that set the specific JPanel(all panels are in the same position in the GridBagLayout, but all start with .setVisible(false);)

jemnuitem1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

        jpanelItem1.setVisible(true);
        jpanelItem2.setVisible(false);
        jpanelItem3.setVisible(false);
    }
});     

jemnuitem2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

        jpanelItem1.setVisible(false);
        jpanelItem2.setVisible(true);
        jpanelItem3.setVisible(false);
    }
});     

jemnuitem3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

        jpanelItem1.setVisible(false);
        jpanelItem2.setVisible(false);
        jpanelItem3.setVisible(true);
    }
});

This works, but I want to know if there is a way better to do this, or can I have a big problem doing this, because if this works, its fine for me work in this way, but I want the help of others that already made something similar.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2582318
  • 1,607
  • 5
  • 29
  • 47

1 Answers1

3

You should use CardLayout. Then you can switch the visible panel instead of writing clumsy code like you have now.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • i saw a little about CardLayout, but they cant auto-resize with the windows change, or can ??? i dont saw that in the Docs, if can, say please so i will change GridBag to CardLayout of course, since i can easily use the ```show (Container parent, String name) Flips to the component that was added to this layout with the specified name, using the addLayoutComponent method.``` – user2582318 Oct 25 '13 at 05:20
  • Why don't you put the GridBagLayout inside the CardLayout? – Kayaman Oct 25 '13 at 05:23
  • nice idea, i can set the contentPane as CardLayoult, add in the TOP of the contentPane a GridBagLayout(because i need that when the user maximize the window, the things inside, resize together), than add under the firstGridBagLayout anothers Panels, and than use the button inside the Grid to change the panels under the buttons using the SHOW? can understand me =x? – user2582318 Oct 25 '13 at 05:44
  • this concept -> http://img62.imageshack.us/img62/8944/uf8z.jpg can you confirm please – user2582318 Oct 25 '13 at 05:49
  • 1
    @user2582318, check out [this example](http://stackoverflow.com/questions/19577104/go-back-to-prior-jpanel/19577478#19577478) with CardLayout here. Check that the panelContainer with `cardlayout`, resize with window re-size as it is added to the jframe content pane with BorderLayout.Center, much like what you want – Sage Oct 25 '13 at 08:30
  • 1
    @user2582318 No, you don't want the CardLayout to be the outmost layout. It should be in the JPanel that is under the Menubar. – Kayaman Oct 25 '13 at 08:40
  • @Sage i got it now, thank you so much for the example, i can understand perfectly now =) Kayaman, thank you tooo =) – user2582318 Oct 25 '13 at 17:07