File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JScrollPane.getViewport().setScrollPosition() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JScrollPane.getViewport().setScrollPosition()" Watch "JScrollPane.getViewport().setScrollPosition()" New topic


Chris Crawford
Ranch Hand

Joined: Jun 22, 2011
Posts: 96

I've been programming Java at a sub-professional level for 6 or 7 years now, and I've managed to bull my way through most problems, but this one has me stumped. I have a JScrollPane with a JTextPane as its child, declared as such:

JTextPane mainTextPane=new JTextPane();
JScrollPane mainScrollPane=new JScrollPane(mainTextPane, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

Its purpose is simply to present a bunch of text. My problem is that, when mainScrollPane is drawn, and the length of the text exceeds the height of mainScrollPane, it presents the text with the scroll bar all the way down. That is to say, the top left corner of mainScrollPane does not show the beginning of the text; instead, the bottom right corner shows the end of the text. Obviously, this is wrong, and I'm surprised at this behavior; it certainly seems to violate normal UI expectations.

So I poked around and found setViewPosition, which is obviously designed to handle any problem like this. So I added this line of code to my window drawing method, immediately after the line that fills mainTextPane with text:

mainTextPane.setText(thisPage.mainText); // fill the JTextPane with the appropriate text
mainScrollPane.getViewport().setViewPosition(new Point(0,0)); // scroll to the top of the text

Much to my surprise, it didn't work. So I started digging around on various Java help websites, and found some other possibilities. I ended up trying all of these possibilities:

mainScrollPane.getViewport().scrollRectToVisible(new Rectangle(0,0,10,10));

mainScrollPane.scrollRectToVisible(new Rectangle(0,0,10,10));

I tried each of them separately; I tried them in combination; I tried them in various sequences. All failed.

Obviously, I told myself, my problem lies with the way I set up mainScrollPane. So I tried a different declaration:

JScrollPane mainScrollPane=new JScrollPane(mainTextPane);

No joy. So I scoured the Internet, looking for discussions of JScrollPane and setViewPosition. There were lots of discussions about this from five or ten years ago, but nothing recent -- apparently everybody in the universe except me has figured this out.

I have noticed something that may include the key to my problem: it seems that the window is drawn once correctly and then immediately redrawn incorrectly. I say this because I can just barely sense that the text is jerking upward when first drawn. Perhaps some Listener somewhere is firing an even that tells mainScrollPane to scroll down. I tried moving this line to the bottom of the window drawing method:

mainScrollPane.getViewport().setViewPosition(new Point(0,0));

Again, this didn't work. So the question is, why is my JScrollPane auto-scrolling to the bottom of the text, and what can I do to get it back up to the top?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46410
Don't know. Sorry. I'll try moving you to our GUIs forum, where they know all about this sort of thing (I hope).

And welcome to the Ranch
Darryl Burke

Joined: May 03, 2008
Posts: 5115

Rob Camick's article on Text Area Scrolling may give you an idea or two.

luck, db
There are no new questions, but there may be new answers.
Chris Crawford
Ranch Hand

Joined: Jun 22, 2011
Posts: 96

Ta-Da! Huzzah and Halleluia! That fixed it! Specifically, Rob Camick's article discusses the idea of the Caret position. Even though I set up my mainTextPane as editable=false, Java automatically sets the caret to the end of the text, then autoscrolls to the caret. By setting the DefaultCaret's UpdatePolicy to NEVER_UPDATE, the bad behavior stopped. My problem is solved!

Thanks so much, fellows!
I agree. Here's the link:
subject: JScrollPane.getViewport().setScrollPosition()
jQuery in Action, 3rd edition