23

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.

Matthew
  • 28,056
  • 26
  • 104
  • 170

5 Answers5

45
textField = new JTextField( ... );
textField.setMaximumSize( textField.getPreferredSize() );
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    This made my textfield tiny horisontally (while sorting the problem vertically). I've got it sorted now (I actually recreated [cbrown's solution](http://stackoverflow.com/a/5573666/2187042) without realising they had posted it) – Richard Tingle Nov 16 '14 at 19:58
  • 1
    @RichardTingle `This made my textfield tiny horisontally` - probably because you didn't specify the text or the number of column when you created the text field. That's what the "..." was for in the parameter Typically for an empty text field you would use code like `"new JTextField(10)"`. Now you will have a reasonable width and height. – camickr Aug 25 '18 at 00:41
32

If you want the width to keep changing, just keep it set to MAX INT. So...

textField.setMaximumSize( 
     new Dimension(Integer.MAX_VALUE, textField.getPreferredSize().height) );
cbrown
  • 321
  • 3
  • 2
1

In my case I need a combination of all the answers for it to work properly. If I don't use glue, it is not centered vertically; if I don't restrict maximum size, it extends vertically; if I restrict both width and height, it is too small, being only wide enough to contain the initialization text.

textField = new JTextField("Hello, world!");
textField.setMaximumSize(
    new Dimension(Integer.MAX_VALUE,
    textField.getPreferredSize().height));
Box box = Box.createVerticalBox();
box.add(Box.createVerticalGlue());
box.add(textField);
box.add(Box.createVerticalGlue());
damix911
  • 4,165
  • 1
  • 29
  • 44
0

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

Randy
  • 16,480
  • 1
  • 37
  • 55
  • 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 Apr 25 '10 at 18:09
  • 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 Apr 25 '10 at 21:11
-2
JPanel panel = new JPanel();

Box box = Box.createVerticalBox();

JTextField tf = new JTextField(8);

box.add(tf);
panel.add(box);

frame.getContentPane().add(panel, BorderLayout.CENTER);
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79