Search code examples
javaswinglayout-managerboxlayout

2 Items in one BoxLayout-Row: one in the left, one in the right


My container JPanel has a boxlayout. In the first row, another JPanel, two elements should be placed: a JLabel in the left and a JButton in the right.

How to place them there?

I can't use BorderLayout (so west and east) for the inner JPanel, because it would make the first row blow up taking the whole height which is really ugly.


Solution

  • In the first row, another JPanel, two elements should be placed: a JLabel in the left and a JButton in the right.

    You can use a horizontal BoxLayout for that and add "glue" between the two components.

    Read the section from the Swing tutorial on Using Invisible Components as Filler for more information.