• Post Reply Bookmark Topic Watch Topic
  • New Topic

Keeping JTextField constrained within the frame

 
abhi narkar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added a JLabel and JTextField to a JPanel which then goes in a JFrame. I have to use GridBagLayout to constraint all these and the other components. I am allowing the JTextField to fill in Horizontally . When I type in a very long string I find that the textarea expands out of the frame i.e. only partly visible other part hides at the rightside edge of the frame. I want that even if very long strings are entered the textarea should not expand, I can display the complete string in a tooltip or allow user to navigate within the textfield using arrow keys. What can I do? I have tried but didn't work.

Thanks.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the problem component - a textfield
or a textarea?
 
abhi narkar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its JTextField. I have made the correction.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I want that even if very long strings are entered the textarea should not expand

I'd suspect you would need to add the textfield to a JPanel at FlowLayout.LEFT,
then add the panel to the GridBagLayout.

if that doesn't work, post a demo program (with just what's in that 'row' of the gridbaglayout),
so we can see exactly what's happening, and tinker with it.
 
abhi narkar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem was, typing into the field didnt give any trouble but when it populated from already saved data which was too long caused the textfield to break constraints. The textfield was being set using setText(String) method call this caused it to resize according to the new strings' length. calling setColumns after setText fixed it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!