This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
posted
0
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: 681
posted
0
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
posted
0
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: 32708
4
posted
0
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
posted
0
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: 681
posted
0
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
posted
0
The idea posted above about setting scrollbar policies worked in my SSCCE:
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.