0

In Java, when using the BorderLayout, is it possible to have two panels in the CENTER, but both be visible on the form.

Here is my code:

    guiFrame.add(guiFieldsPanel, BorderLayout.CENTER);
    guiFrame.add(guiButtonsPanel, BorderLayout.CENTER);        
    guiFrame.setVisible(true);

In the above code, both panels are set to the center, yet I can only see the guiButtonsPanel as it is 'on top' of the guiFieldsPanel.

Can i group both panels together, and then set them to be displayed in the CENTER?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2351151
  • 725
  • 4
  • 12
  • 16
  • `BorderLayout` by default, only allows a single component to reside within any of its 5 available positions. If you try to add another component to an occupied position, you are effectively removing the previous component from the component (it isn't actually removed, it's just not laid out, but the result is the same...) – MadProgrammer May 12 '13 at 07:08

2 Answers2

5

See the Nested Layout Example for ideas about how to combine layouts to create the required layout. E.G.

Perhaps use a single row GridLayout for the center.

guiFrame.add(guiFieldsPanel, BorderLayout.CENTER);
guiFrame.add(guiButtonsPanel, BorderLayout.CENTER);        

But that suggests a 2 column GroupLayout as seen in this answer. E.G.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

You will need to create an intermediate panel that will contain both guiFieldsPanel and guiButtonsPanel, and then add that to the border layout.

final JPanel centre = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
centre.add(guiFieldsPanel);
centre.add(guiButtonsPanel);

guiFrame.add(centre, BorderLayout.CENTER);
guiFrame.setVisible(true);

You can adjust the layout of centre as appropriate for your needs with respect to the relative positioning of guiFieldsPanel and guiButtonsPanel.

Greg Kopff
  • 15,945
  • 12
  • 55
  • 78