Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set Constant Size for JTextField in GridBag Layout

Status
Not open for further replies.

YerMom

Programmer
Oct 3, 2006
127
US
I have a JPanel that uses the GridBag layout to position controls. One of the controls is a JTextField. I create it with the following code:

JTextArea ta = new JTextArea("default text");
ta.setBorder( new LineBorder( Color.blue, 1) );
ta.MiminumSize( new Dimension(20,20) );

Regardless of the default text and minimum size, the initial height and width of the JTextField are the same.

Can anyone tell me how I get the JTextField dimensions to be constant?

Thanks!
 
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
Code:
//A text area with default text of "Hello" and with 3 rows and 10 columns
JTextArea textArea = new JTextArea("Hello", 3, 10);

Tim
 
Tim said:
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

I guess someone overnighted :p

The second "JTextField" should be "JTextArea"

Cheers,
Dian
 
Thank you Tim and Dian.

I overnighted too - the title of my post should have referenced a JTextArea. (I originally was going to use a JTextField for my solution.)

I tried using the constructor where you specify rows and columns, but the control still resized itself.

You're right - the GridBagLayout is complex. I'm going to try a null layout manager, but I sure would like to know how to use grid bag and achieve my desired effect.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top