0

I am using BoxLayout to layout 3 components in a row horizontally - two JLists (J1 and J2) and a JPanel in between (X).

---------------
|             |
|             |
|  J1  X  J2  |
|             |
|             |
---------------

The problem I am having is that BoxLayout is making each column equal size, but what I want is for X to be its smallest size and J1 and J2 to take up all the available horizontal space. How can I do this? Basically, the look I am going for is something like this.

enter image description here

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
XåpplI'-I0llwlg'I -
  • 21,649
  • 28
  • 102
  • 151
  • 2
    Post SSCCE. As far as I know Box Layout allows different sizes of columns. – StanislavL Apr 30 '12 at 10:15
  • What is the preferred size of `X`? – trashgod Apr 30 '12 at 10:35
  • Why do this need a SSCCE? It's two JLists separated by a middle component, all using the same layout manager. Can't get more clearer than that. – XåpplI'-I0llwlg'I - Apr 30 '12 at 11:44
  • If you post an SSCCE, I (& possibly others) can play with it to check answers before posting them. But hey, it's your problem, so if you are not that interested in an answer, don't bother. BTW - I don't believe @StanislavL would have been notified of your comment. Add `@PersonName` to ensure they are notified. – Andrew Thompson Apr 30 '12 at 13:16

4 Answers4

1

I would use a GridBagLayout.

The center buttons would be contained in a JPanel.

The JList, button JPanel, and JList would be contained in 1 row with 3 elements across the row.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Yes, I ended up doing this. I made both JLists have a `weightx`/`weighty` of 1 and a `fill` of `BOTH`; I made the middle panel have a `weightx`/`weighty` of 0 and a `fill` of `NONE`. – XåpplI'-I0llwlg'I - May 01 '12 at 10:36
0

Put a horizontal glue between the JLists:

box.add(Box.createHorizontalGlue())
Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
-1

You should use the setPreferredSize and setMinimumSize methods. These can be used in conjunction with this method to base your pane size off the screen size:

screen Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

although you might just have your window as a fixed size.

For the lists you should add the items to a JPanel, set the size of the panel based on the number of objects and then set the JScrollPane view to that JPanel with setViewPort to ensure your scroll pane is the right size.

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
Conor Pender
  • 1,071
  • 1
  • 14
  • 30
  • 1
    See also [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing](http://stackoverflow.com/q/7229226/230513)? – trashgod Apr 30 '12 at 10:35
-1
    JPanel panel_20 = new JPanel();
    tabbedPane_2.addTab("New tab", null, panel_20, null);
    panel_20.setLayout(new BoxLayout(panel_20, BoxLayout.X_AXIS));

    JPanel panel_24 = new JPanel();
    panel_24.setBorder(new EmptyBorder(3, 3, 3, 3));
    panel_20.add(panel_24);
    panel_24.setLayout(new BorderLayout(0, 0));

    JList list_7 = new JList();
    list_7.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    panel_24.add(list_7);

    JPanel panel_25 = new JPanel();
    panel_20.add(panel_25);
    panel_25.setLayout(new BoxLayout(panel_25, BoxLayout.Y_AXIS));

    JButton btnNewButton_4 = new JButton(">");
    panel_25.add(btnNewButton_4);

    JButton btnNewButton_5 = new JButton("<");
    panel_25.add(btnNewButton_5);

    JPanel panel_26 = new JPanel();
    panel_26.setBorder(new EmptyBorder(3, 3, 3, 3));
    panel_20.add(panel_26);
    panel_26.setLayout(new BorderLayout(0, 0));

    JList list_8 = new JList();
    list_8.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    panel_26.add(list_8, BorderLayout.CENTER);