• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextField in JScrollPane

 
Norm Radder
Bartender
Posts: 1241
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to learn Swing GUI again and have a problem.
When the following program displays its window initially, there is a text field in the middle full of characters. If I add a character to it, the scroll bar appears and obscures the text in the text field. I've tried some changes to the GridBagConstraints that give the text field more room than it need initially so that when the scroll bar is added, the text is still visible.
However, this is rather poor looking. And if after adding an extra character to the text field, I click on the window, the text field is squeezed down to only show a few characters.
Question:
How to have the layout manager re-do the layout when the scroll bar is added so that the text field has its contents appear the same size? Setting ipady gives the text field too much space without the scrollbar.

Thanks,
Norm
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't know if this will suit, but try setting the scrollbar policies
JScrollPane jsp = new JScrollPane(encStrTF,ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

but is there a particular reason you want the scrollpane?
on it's own, the textfield responds to end/home/arrow keys.

also, unless you're just trying a few things:

addWindowListener(new WindowAdapter() {.........
should be replaced by
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

and

Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((ss.width-getBounds().width)/2,(ss.height-getBounds().height)/2);
should be replaced by
setLocationRelativeTo(null);
 
Norm Radder
Bartender
Posts: 1241
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.
What about the
if after adding an extra character to the text field, I click on the window, the text field is squeezed down to only show a few characters.

What causes that?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave up ages ago trying to understand why gridbaglayout does what it does.

I find it much easier to nest a couple of layouts to achieve the same effect.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49860
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using GridBag you might need to do one or all of these to your text field:
  • Set its preferred size
  • Set an x-weight in the constraints
  • Set a horizontal (or both) fill in the constraints.
  •  
    pete stein
    Bartender
    Posts: 1561
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I too try to assiduously avoid GridBagLayout, and from what I can tell, this is not a GridBagLayout problem since I was able to reproduce the problem (but no solution, sorry) by using other layouts:

    [ August 31, 2008: Message edited by: pete stein ]
     
    Norm Radder
    Bartender
    Posts: 1241
    14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the responses.
    Glad to see its not just me.
    Its taken hours so far for a small simple gui.
    But you've all said I MUST learn Swing, so I'll continue on.
     
    pete stein
    Bartender
    Posts: 1561
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The idea posted above about setting scrollbar policies worked in my SSCCE:

     
    Avi Abrami
    Ranch Hand
    Posts: 1141
    1
    Java Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Norm,
    Here's a silly idea: Use JTextArea instead of "JTextField" with line-wrap set.

    Good Luck,
    Avi.
     
    Craig Wood
    Ranch Hand
    Pie
    Posts: 1535
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Setting this up so that the textField expands/contracts with user input seems a bit unusual. Usually we seem to gravitate toward using a DocumentFilter for this kind of input–restriction behavior. But it is a fun thing to explore. Here is one way you might put it together:
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic