5

When adding 2+ buttons to east layout, only 1 shows. I am trying to test a layout that uses tabbed panes. For some reason when I try to add multiple buttons to the east region, it only shows 1 button. It just so happens the button displayed is the last one added to the east region, the rest are ignored. I am thinking maybe they are just hidden underneath the last button.

public void createPage1()
    {
        {
            panel1 = new JPanel();
            panel1.setLayout( new BorderLayout() );

            panel1.add( new JButton( "North" ), BorderLayout.EAST );
            panel1.add( new JButton( "South" ), BorderLayout.EAST );
            panel1.add( new JButton( "East" ), BorderLayout.EAST );
            panel1.add( new JButton( "West" ), BorderLayout.EAST );
            panel1.add( new JButton( "Center" ), BorderLayout.EAST );
        }
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
jerhynsoen
  • 205
  • 1
  • 7
  • 20
  • You realize you're adding all to EAST right ? – Aviram Segal Jul 26 '12 at 16:21
  • See also the [nested layout example](http://stackoverflow.com/questions/5621338/how-to-add-jtable-in-jpanel/5630271#5630271) for an illustration how you can nest layouts, which you will need to use if you want to add multiple buttons in the EAST – Robin Jul 26 '12 at 16:25

3 Answers3

14

I dont know, how you want your UI to look like, but try it this way:

public void createPage1() {
    //This will be the main panel. 
    //We are going to put several buttons only in the "EAST" part of it.

    panel1 = new JPanel();
    panel1.setLayout( new BorderLayout() );

    //We create a sub-panel. Notice, that we don't use any layout-manager,
    //Because we want it to use the default FlowLayout
    JPanel subPanel = new JPanel();

    subPanel.add( new JButton( "1" ));
    subPanel.add( new JButton( "2" ));
    subPanel.add( new JButton( "3" ));

    //Now we simply add it to your main panel.
    panel1.add(subPanel, BorderLayout.EAST);
}
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
7

BorderLayout only allows one component per section. If you want to keep BorderLayout, but have 2+ buttons, I suggest first putting each of those buttons into a JPanel and then putting that JPanel into the east slot.

However, there are probably much better layout choices for you. You also mention tabs, which there is already JTabbedPane for.

Check out the different LayoutManagers, and try to figure out which one is right for you.

Rob Wagner
  • 4,391
  • 15
  • 24
4

1. The default layout for JFrame is BorderLayout, and it has East, West , North , South, and Center area, out of which Center is the default if positioning is not mentioned.

2. Now each section/area can hold only one widget (ie. swing component).

3. You will have this done in much better way by using Group Layout, which was developed by NetBeans team in 2005, use Windows Builder Pro, now free from google.

4. But if you still want to go with the BorderLayout, i will suggest you to use JPanel on the content pane of the JFrame to add Buttons in the manner you want......

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75