Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes JTextField preferred size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField preferred size" Watch "JTextField preferred size" New topic
Author

JTextField preferred size

Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
I have a custom component, and I would like to set it's preferred height to the height of a JTextField. One way is to simply print out the preferred height of JTextField and use that number everywhere. Since I think this value not a constant(depend on platform,...) it not looks a good solution. Another possibilty is to create a JTextField at runtime and use it's height. That not sounds good also.
What is the proper way to achieve what I want?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

In one of my utility classes I have the following line:
I create one JTextField, retrieve its preferred size, retrieve that size's height, and both the size (Dimension) and the JTextField are eligible for garbage collection immediately. What's the problem with that?

Of course this will cause problems if you change the UI after retrieving the height, but you could make the field private and non-final with a method for updating the field.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Thanks Rob.
 
wood burning stoves
 
subject: JTextField preferred size
 
Similar Threads
Positioning scroll bar
JTable width and height
Border Layout
Box Layout doubts
Width issue with JTextField and JComboBox in GridBagLayout