*
The moose likes Swing / AWT / SWT and the fly likes Set scrollbar to top Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Set scrollbar to top" Watch "Set scrollbar to top" New topic
Author

Set scrollbar to top

Rebecca Witmer
Ranch Hand

Joined: Sep 10, 2004
Posts: 46
I want my vertical scroll bar to initialize to the top but it always seems to start somewhere a few pixels from the bottom. I thought that this was the way to do it...

(html view is a JScrollPane)
htmlView.getVerticalScrollBar().setValue(0);
int val = htmlView.getVerticalScrollBar().getValue();
System.out.println("Value of scrollbar is " + val);

...but it always prints 0 while putting the scroller near the bottom. The docs seem to suggest that 0 is the value for "top". What did I do wrong? Thanks for any thoughts.


SCJP 1.4
Rebecca Witmer
Ranch Hand

Joined: Sep 10, 2004
Posts: 46
Okay, so I have been pondering this some more and I've come up with a lot of confusing data. Is there another way to do this scrollpane stuff? I am baffled. Maybe someone else won't be. Here's what I've come up with:

There are two scroll panes side-by-side in my gui. The left one has a tree of stuff you can click on to see metadata about it in the left side. Here's some code I have for setting up the panes (partially):



So that's at the beginning. When it first sets up the panels it always tells me:
Value of scrollbar is 0
Value of scrollbar is 0
That's right because the default is for there to be no vertical scrollbar unless the text stretches it out. Since there's no text yet, no scrollbar. Thus its value is 0.

Then I have added this to the code that runs when you click something on the left. I tried all _kinds_ of stuff to get it to behave:



But nothing works. The scroll bar stays not at the very bottom, but one or two pixels away, exactly at the bottom of the final character. Here's some output:

Value of scrollbar is 0 <---Initial values
Value of scrollbar is 0


Value of scrollbar before value changed is 0 <---After I clicked something
Value of scrollbar after value changed once is 0
Maximum is 295
Minimum is 0
Value of scrollbar after value changed twice is 0


Value of scrollbar before value changed is 252 <---The 2nd thing clicked
Value of scrollbar after value changed once is 0
Maximum is 295
Minimum is 0
Value of scrollbar after value changed twice is 0


Value of scrollbar before value changed is 86 <---Dragged the s.b. to the
Value of scrollbar after value changed once is 0 <---middle then clicked
Maximum is 295
Minimum is 0
Value of scrollbar after value changed twice is 0

If you take out setting the size of the JEditorPane to 295 and just let it figure out its own size it will say that the value of the scrollbar is 25 or 40 depending whether you add on another "Y" or not. That is, it sets the size and scroll bar value to match the amount of text.

I don't get why it wants to be at the bottom but at this point I am so baffled that I am willing to trash this idea and try another way. Is there another elegant way to do this?

Thanks,
Rebecca
 
 
subject: Set scrollbar to top