views:

260

answers:

2

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.

A: 

set the max height. or put them in a scroll region

Randy
Thanks, Randy--is this really the best way of doing it? It seems the height should be different depending on the user's font size. Or does Swing not respect system fonts? Also, I can't seem to find a way to set the maximum height without also setting the width (which I want to remain automatic). The closest thing I can find is `setMaximumSize`, which takes a `Dimension` (width AND height) as its parameter.It just seems like there MUST be a better way.
Matthew
its alot about the layout manager. boxlayout vs gridlayout vs my favorite borderlayout. you should play with how the fileds behave in all those - you may find one you like.
Randy
+1  A: 
textField = new JTextField( ... );
textField.setMaximumSize( textField.getPreferredSize() );
camickr