• Post Reply Bookmark Topic Watch Topic
  • New Topic

Limiting the size of a JTextField  RSS feed

 
Jim Henessey
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm having major issues with my JTextFields blowing up in size, to fill an entire panel (or sometimes exceeding the width of the panel). I set the length of the field to a reasonable number, and set the preferredSize and maximumSize of both the field and the containing panel. Yet I still have problems with the JTextField either being too small (I put in a very small number, say 5, in as the textfield length) or blowing up to an unreasonable size (any field length that would be needed for form input, 20-40 characters).

What am I doing wrong that the textfield size is so out-of-control?

Thanks.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What LayoutManager are you using? The LayoutManager will determine how (or if) the preferred size has any influence on the actual size of the text field as it is displayed. I've found that if you GridBagManager typically gives you the most control over how the components are displayed.

I hope this helps to get you in the right direction. If you need more help, perhaps you can post some code snippets here so we can see exactly what you are trying to do.

Keep Coding!

Layne
 
Jim Henessey
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help on this. I'm using a BorderLayout manager inside a GridLayout. Gridlayout is being because most of it consists of two columns - a label with an input field next to it. The BorderLayout is used for a list of input fields for placing in song and artist information - I want the first box right-aligned and the second left-aligned.

One possibility is to use a simpler layout here - list a series of panels using BoxLayout, and then distribute the input fields with a simple FlowLayout, making sure there's not too much space between them by keeping the width of the individual panels narrow.

I've listed a small code sample below. Whether or not this gives you anything else to respond to, thanks for the help so far.

 
Mike Litherland
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How odd, I just stopped by the ranch to look for an answer to this exact same topic. I'm having the problem with BoxLayout, even if I tack some glue on the bottom of it. Here's some sample code that illustrates the problem:



Doing a setPreferredSize has no effect on that JTextField size. I'd rather avoid a gridbag layout, but I'm starting to think that's the only way to handle this.

Any ideas?

Mike

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!