Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Set Constant Size for JTextField in GridBag Layout

YerMom (Programmer)
7 Nov 06 16:32
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!
timw (Programmer)
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

CODE

//A text area with default text of "Hello" and with 3 rows and 10 columns
JTextArea textArea = new JTextArea("Hello", 3, 10);

Tim

Diancecht (Programmer)
8 Nov 06 3:35

Quote (Tim):


 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
timw (Programmer)
8 Nov 06 5:01
blush Thanks, Dian.

Tim

YerMom (Programmer)
8 Nov 06 8:36
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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close