2

I have a JLabel, a JButton, and a JTextField all arranged horizontally in that order on a JPanel. The JTextField does not initially have any text in it. The JLabel and JButton each display with their natural width. However, the width of the JTextField is quite small. (It then expands when the user enters text.)

How can I make the JTextField use all the remaining width of the JPanel left over from the JLabel and JButton and have it retain that width regardless of the text this is entered?

Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
  • 3
    If `JTextField(int columns)` isn't sufficient, `BoxLayout` might do. – trashgod Feb 13 '12 at 17:54
  • BoxLayout is solving the problem except that now the JTextField is as tall as the JPanel containing it (it doesn't need to be that tall.) – Paul Reiners Feb 13 '12 at 20:16
  • See also [*Specifying Component Sizes*](http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#size). – trashgod Feb 13 '12 at 21:59

1 Answers1

2

I have never used BoxLayout so it may do the trick. However, and for sure, GridBagLayout can do it.

Set the GridBagConstraints to gridx=0 , 1 , 2, (respectively for component 1, 2 and 3) gridy=0 (for all of them) and weightx=0 for the first two and weightx=1.0 for the last component.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117