8 Nov 06 2:53
Don't use a GridBagLayout, possibly?
In general, the layout managers impose a positioning and sizing upon the components they manage. That's their job, that's how they work.
You can use null
as your LayoutManager, which is to say, you specify not to have one. Components then stay where you put them and with the size you specify. The downside is that you don't get that nice re-positioning of all the controls when the panel they are on is resized by the user.
Certain areas dictated by various layout managers allow some of the component metrics to work normally. For example, if you put a JTextField in the CENTER of a BorderLayout, it will swell to fill that area vertically and horizontally, but if you put it in the NORTH, then the text field's height will be honoured but it will still swell to fill the space horizontally. The WEST and EAST areas work the exact opposite. You have to read the API docs on the particular LayoutManager to work out how components will behave. The GridBagLayout is one of the most complex layout managers in the Swing set.
Your post mentions that you want to control a JTextField, but the code example creates a JTextArea. They are different components. JTextField can take a value for the number of 'columns' in its contructor, whereas the JTextField, being a multi-line component, can take rows and columns. Depending on how you're putting your component into the layout, trying the row / column contructor might give you what you want
//A text area with default text of "Hello" and with 3 rows and 10 columns
JTextArea textArea = new JTextArea("Hello", 3, 10);