Java Swing How to - Make a component fill all available space in a row with BorderLayout








Question

We would like to know how to make a component fill all available space in a row with BorderLayout.

Answer

import java.awt.BorderLayout;
//from   w w w.jav a  2 s .c o m
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

public class Main {

  public static void main(String[] args) {

    JPanel ui = new JPanel(new BorderLayout(2, 2));
    ui.setBorder(new EmptyBorder(4, 4, 4, 4));

    JPanel controls = new JPanel(new BorderLayout(2, 2));
    ui.add(controls, BorderLayout.PAGE_START);
    String s = new String(Character.toChars(8594));
    String[] items = { "Choice: right " + s + " arrow" };
    JComboBox cb = new JComboBox(items);
    controls.add(cb, BorderLayout.CENTER);
    controls.add(new JButton("Button"), BorderLayout.LINE_END);

    JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JTextArea(4,
        40), new JTextArea(4, 40));

    ui.add(sp, BorderLayout.CENTER);

    JFrame f = new JFrame("Stretch Combo Layout");
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setContentPane(ui);
    f.pack();
    f.setLocationByPlatform(true);
    f.setVisible(true);

  }
}