Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 719
Joel Avatar asked Jun 29 '11 13:06

Joel


2 Answers

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.

like image 171
Heisenbug Avatar answered Sep 21 '22 15:09

Heisenbug


OP asked how he can make BoxLayout respect vertical glue with JTextFields only. The text field maximum height can be set from getPreferredSize().

However, in my case I also need JTextArea, and the situation gets more weird: JTextArea and vertical glue seem to share the remaining height together, although I have limited the maximum height of the JTextArea.

The code below fixes the OP problem with JTextFields, but the problem with JTextArea remains (when you uncomment the commented part you will see some "funny" behavior).

import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
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();

        JTextField field1 = new JTextField();
        JTextField field2 = new JTextField();
        field1.setMaximumSize(new Dimension(Integer.MAX_VALUE, field1.getPreferredSize().height));
        field2.setMaximumSize(new Dimension(Integer.MAX_VALUE, field2.getPreferredSize().height));
        b.add(field1);
        b.add(field2);

//        JTextArea area = new JTextArea();
//        area.setRows(4);
//        area.setMaximumSize(new Dimension(Integer.MAX_VALUE, area.getPreferredSize().height));
//        b.add(new JScrollPane(area));

        b.add(Box.createVerticalGlue());

        f.setContentPane(b);
        f.setSize(500, 200);
        f.setVisible(true);
    }
}
like image 23
homunculus Avatar answered Sep 23 '22 15:09

homunculus