File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Problems when using JScrollPane to scroll JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problems when using JScrollPane to scroll JTextArea" Watch "Problems when using JScrollPane to scroll JTextArea" New topic
Author

Problems when using JScrollPane to scroll JTextArea

Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
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);

JPanel allPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.gridy=12;
allPanel.add(new JScrollPane(mySynopsis, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), gbc);

gbc.gridy=13;
allPanel.add(new JScrollPane(myComments, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), gbc);

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.

TIA.
Michael Dunn
Ranch Hand

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

Joined: Sep 24, 2008
Posts: 323
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.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> How would I give the scrollpane a preferred size?

scrollpane.setPreferredSize(...);

> 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 GridLayout JPanel
textArea/scrollPane in a BorderLayout JPanel (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
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems when using JScrollPane to scroll JTextArea