I have a gui where I'm displaying several labels and text fields. I got the JTextArea's to wrap the way I want them to. However, if there was enough information, they didn't show it all. So I've added scroll bars. However, when the scroll bar appears, it stretches the window. So instead of scrunching the text some, I get the same text size with a scroll bar tacked onto the end. I assume it has something to do with the viewport size, but I'm not sure how to dynamically change it or even if that's what I need to do.
Here's some of the relevant code.
mySynopsis = new JTextArea(3, 30); mySynopsis.setEditable(false); mySynopsis.setLineWrap(true); mySynopsis.setWrapStyleWord(true); myComments = new JTextArea(3, 30); myComments.setEditable(false); myComments.setLineWrap(true); myComments.setWrapStyleWord(true);
The other components are all laid out the way I want them to be. And these two fields work fine when the text doesn't require scrolling.
Also, is there a way to set the scroll bars to default to the top of the text, and not the bottom? Right now if I want to see what's at the top of the field, I have to manually scroll up, vs scrolling down if I want to see more.
> is there a way to set the scroll bars to default to the top of the text, and not the bottom?
after adding the text, add this line [textArea].setCaretPosition(0);
as for the scrollpane problem, I avoid GridBagLayout because of its ripple effect, fixing one thing breaks 2 more. Often scrollpane problems can be fixed by giving the scrollpane a preferredSize. That's unlikely to fix it in a GBL, but worth a try.
Joined: Sep 24, 2008
Cool. I'll check out the setCaretPosition.
How would I give the scrollpane a preferred size? I want it to be the same size as the text area without the scroll bars.
And what layout would you suggest? I originally used Grid, but I want my text areas to be bigger than the other fields, and that's why I switched to GBL.
Joined: Jun 09, 2003
> How would I give the scrollpane a preferred size?
> I want it to be the same size as the text area without the scroll bars.
you could try scrollpane.setPreferredSize(textArea.getPreferredSize());
but the above can be overridden by gridbagconstraints.FILL
> And what layout would you suggest? I originally used Grid, but I want my > text areas to be bigger than the other fields, and that's why I switched to GBL.
would need to see a working example of the current layout to suggest something else. nesting panels is the usual way of avoiding gridbaglayout e.g all you labels/textfields in a GridLayoutJPanel textArea/scrollPane in a BorderLayoutJPanel (at borderlayout.CENTER) add the gridLayoutPanel to the frame's borderLayout.WEST add the borderLayoutPanel to the frame's borderLayout.CENTER
you could leave the borderLayoutPanel as a default FlowLayout, the difference would be when the frame is dragged wider or taller: flowLayout will take up the additional space borderlayoutPanel will increase the size of the scrollpane/textArea
Joined: Sep 24, 2008
Ah. Well, I ended up getting it to work, but not exactly the way I wanted. I set the scollbar to always, and now it displays correctly.