• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField size  RSS feed

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a (subclass of) JTextField in my application. I construct it using the constructor that takes an integer number of characters, to control the size of the field.



Normally, this works fine. But when there is not enough space for the field at the chosen size, it snaps to a tiny size. I would like it to shrink just enough to fit in the available space.

I am using GridBagLayout. Here's where it gets put in the layout: -

 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GridBagLayout tries to show components at their preferredSize. When the preferredSize is greater than the available display area for the component the component is shown at its default size (collapsed). To get a fluid layout you can use a non–zero value for weightx and HORIZONTAL for the fill constraint. Testing shows that this will display the textField even at display area widths less than that required for the columns argument. Fluid layouts can help avoid component sizing issues.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that works well.

The documentation (in Java 1.4) doesn't exactly make it clear that this would be expected to work. It talks about "fill" being for controlling what happens when there is too much space available, not too little. Ho hum.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!