wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JTextField in JScrollPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField in JScrollPane" Watch "JTextField in JScrollPane" New topic
Author

JTextField in JScrollPane

Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
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

Joined: Jun 09, 2003
Posts: 4632
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
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

    Joined: Feb 23, 2007
    Posts: 1561
    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
    Ranch Hand

    Joined: Aug 10, 2005
    Posts: 685
    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

    Joined: Feb 23, 2007
    Posts: 1561
    The idea posted above about setting scrollbar policies worked in my SSCCE:

    Avi Abrami
    Ranch Hand

    Joined: Oct 11, 2000
    Posts: 1132

    Norm,
    Here's a silly idea: Use JTextArea instead of "JTextField" with line-wrap set.

    Good Luck,
    Avi.
    Craig Wood
    Ranch Hand

    Joined: Jan 14, 2004
    Posts: 1535
    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:
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: JTextField in JScrollPane