Search code examples
javaswinglayoutboxlayout

Swing BoxLayout problem - Can't make the Fillers do their job


What i'm trying to do

In Swing, I'm trying to use a BoxLayout or equivalent linear container, but the items in the container are stretching vertically. Inside my application, I don't want them to stretch vertically.

I know i could set a preferredSize or maximumSize on components, but the following code is just a reproducer, and I can't hard-code or maximize the size of the components, which are in reallity more complex and dynamic. And I just can't use a BorderLayout with the BorderLayout.TOP position, because no scroll bars will ever show if I do that. And I might need scroll panes.


What I have tried

So I tried to use the fillers in a BoxLayout as explained in Using Invisible Components as Filler , but it just doesn't work. Although in the Oracle documentation, it seemed to be exactly what I needed. Here are my attempts:

    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;


    public class TestBoxLayout implements Runnable {

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new TestBoxLayout());
        }

        @Override
        public void run() {
            JFrame f = new JFrame("test box layout");

            JPanel b = new JPanel();
            b.setLayout(new BoxLayout(b, BoxLayout.PAGE_AXIS));

            b.add(new JTextField("field 1"));
            b.add(new JTextField("field 2"));
            b.add(new JTextField("field 3"));
            b.add(Box.createVerticalGlue());

            f.setContentPane(b);
            f.setSize(500, 200);
            f.setVisible(true);
        }

    }

This is the result I get:

Fillers are not working


Second try

I tried to use the Box class instead of JPanel with BoxLayout, but the visual result is exactly the same. Here is my second try:

    import javax.swing.Box;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;


    public class TestBox implements Runnable {

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new TestBox());
        }

        @Override
        public void run() {
            JFrame f = new JFrame("test box");

            Box b = Box.createVerticalBox();

            b.add(new JTextField("field 1"));
            b.add(new JTextField("field 2"));
            b.add(new JTextField("field 3"));
            b.add(Box.createVerticalGlue());

            f.setContentPane(b);
            f.setSize(500, 200);
            f.setVisible(true);
        }

    }

What I would like to do

Does anyone know how I can fix those fillers and make them work ? A fix on the given code would be fantastic.

This is a drawing I made using paint, which shows what I'd like to have as a result: Expected result


Solution

  • BoxLayout is fine for what you are trying to do. The glue insert as last component is fine. Why aren't simply using setMaximumSize methods on your JTextFields? I'm not sure JTextFields alone can satisfy your needs. If using setMaximumSize don't work put each JTextField Inside a JPanel and then use setMaximumSize on each JPanel.