How do I keep JTextFields in a Java Swing BoxLayout from expanding?

Posted by Matthew on Stack Overflow See other posts from Stack Overflow or by Matthew
Published on 2010-04-25T17:48:03Z Indexed on 2010/04/25 17:53 UTC
Read the original article Hit count: 221

Filed under:
|
|
|

I have a JPanel that looks something like this:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

...

panel.add (jTextField1);
panel.add (Box.createVerticalStrut(10));
panel.add (jButton1);

panel.add (Box.createVerticalStrut(30));

panel.add (jTextField2);
panel.add (Box.createVerticalStrut(10));
panel.add (jButton2);

... //etc.

My problem is that the JTextFields become huge vertically. I want them to only be high enough for a single line, since that is all that the user can type in them. The buttons are fine (they don't expand vertically).

Is there any way to keep the JTextFields from expanding? I'm pretty new to Swing, so let me know if I'm doing everything horribly wrong.

© Stack Overflow or respective owner

Related posts about java

Related posts about swing