Meaningless Drivel is fun!
The moose likes Swing / AWT / SWT and the fly likes JTextField.setSize()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField.setSize()?" Watch "JTextField.setSize()?" New topic


henry akoma

Joined: May 17, 2005
Posts: 21

please can someone help me understand why the method setSize(int w, int h) does not set the size of a JTextField:

JTextField.setSize(int w,int h)

And please, what method can do this.

David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Is your component inside a panel with a layout manager? A layout manager will reset the size of your component based on various constraints. You can use the setPreferredSize() method instead of setSize() to tell most layout managers what size the field should be.
Tom McC

Joined: Jul 28, 2005
Posts: 21
If what David said doesn't do what you want it to, check out the setColumns(int) method of JTextField; It should set how many characters you want to be able to fit in the field, which I believe will make it longer.

Brad Bahls

Joined: Aug 21, 2009
Posts: 1
this thread is a bit old but here's a good explanation of how gridbag can mess with a JTextField. The link also has many good hints on other gridbag goodies.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
Brad Bahls wrote:this thread is a bit old . . .
True. There is a good chance the original poster is no longer reading. I liked the link, but the constraints class of Cai Horstmann's seems better.

And welcome to JavaRanch
I agree. Here's the link:
subject: JTextField.setSize()?
It's not a secret anymore!