Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when adding 2+ buttons to east layout, only 1 shows

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 );
        }
    }
like image 914
jerhynsoen Avatar asked Jul 26 '12 16:07

jerhynsoen


2 Answers

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);
}
like image 51
Balázs Édes Avatar answered Nov 05 '22 06:11

Balázs Édes


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.

like image 7
Rob Wagner Avatar answered Nov 05 '22 05:11

Rob Wagner