jerhynsoen jerhynsoen - 5 months ago 12
Java Question

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 );
}
}

Answer

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);
}
Comments