Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextField preferred size

 
Miklos Szeles
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Miklos Szeles
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic