1

In my application, there are 4 panels. And i need to insert them into the main panel, which uses BorderLayout. The 4 panels are...

  1. A thin Image strip.
  2. 4 buttons just below above
  3. A TextField covering the complete page.
  4. An about at end.

This is my code...

    add(imageLabel, BorderLayout.NORTH);
    add(buttonPanel,BorderLayout.PAGE_START);
    add(logScrollPane, BorderLayout.CENTER);
    add(about, BorderLayout.PAGE_END);

When I do this, the buttonPanel disappears. How can I achieve what I need?

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Jatin
  • 31,116
  • 15
  • 98
  • 163

1 Answers1

6

I usually try to keep a maximum of 3 components in any BorderLayout, so I would do it like this...

JPanel outerPanel = new JPanel(new BorderLayout());
JPanel innerPanel= new JPanel(new BorderLayout());

innerPanel.add(buttonPanel,BorderLayout.NORTH);
innerPanel.add(logScrollPane, BorderLayout.CENTER);
innerPanel.add(about, BorderLayout.SOUTH);

outerPanel.add(imageLabel, BorderLayout.NORTH);
outerPanel.add(innerPanel,BorderLayout.CENTER);

As long as you keep the 'maximum-stretched' component in the CENTER (in this case, your logScrollPane) then it'll always work. If you want to use the panel, such as setting it on a JFrame, just use add(outerPanel).

Don't be afraid of BorderLayout - the ability of this layout to auto-expand the CENTER component to fill the available space make it a very powerful and very important LayoutManager!

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • Thanks :). Swing does take a toll. There should be an mvc like framework to make applciation quickly. Netbeans GUI is still a major help – Jatin Oct 31 '12 at 12:36
  • @Jatin: See also this [MVC outline](http://stackoverflow.com/a/2687871/230513) and [example](http://stackoverflow.com/a/3072979/230513). – trashgod Oct 31 '12 at 13:01