Search code examples
javaswinglayout-managerborder-layout

Java JPanel in BorderLayout positioning


Can I expand a child panel all the way from BorderLayout.LINE_START to BorderLayout.LINE_END, fill all the space from right to left including the center ?

I am trying to accomplish as below...

+-------------------------+
|                         |
+-------------------------+
|     PANEL OCCUPYING     |
|         ALL THIS        |
|          SPACE          |
+-------------------------+
|                         |
+-------------------------+

Solution

  • That looks like the CENTER of the BorderLayout.

    Read the section from the Swing tutorial on How to Use BorderLayout for more information and examples.

    for some reason, I keep getting empty spaces on the right and left...

    The CENTER will occupy all the space available. So it depends on the component that you add to the CENTER. Maybe you are adding a panel and the panel expands, but the component on the panel doesn't expand because by default a panel uses a FlowLayout and FlowLayout respects the preferred size of the components added to it.