Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JScrollPane, when setting text, how to set scrollbar to the top?

Status
Not open for further replies.

bigDaftie

Programmer
Feb 28, 2003
36
GB
I am using a JScrollPane. When I set the text, the pane is at the bottom, showing the last of the data I sent to the pane. I want the scrollbar to be at the top otherwise the user has to scroll up to see the title and the top of the page. This is something that's been bugging me for a while now and I can't seem to find a solution, so help gratefully recieved.
Thanks
bd
 
I think something along the lines of
Code:
class GUI
{
.
.
.
   // create the scrollpane
   JScrollPane sp = new JScrollPane();

   // get the viewport (the visible "window" in the scroll pane)
   JViewport vp = sp.getViewPort();

   // set the viewport to the upper left hand point.
   // ** I cant remember if 0,0 is the top left or bottom left
   // ** So you may need to change this
   vp.setViewPosition(new Point(0,0));

   // of course this works to -
   //    sp.getViewPort().setViewPosition(new Point(0,0));
   // if you want to do it all on one line.
.
.
.
}

This may be what you are after, let me know how it goes.

-------------------------------------------
There are no onions, only magic
-------------------------------------------
 
Thanks a lot for getting back to me. This does look right but I can't seem to get it to work!

This is what I'm doing in the class constructor

display = new JEditorPane("text/html"," ");
JScrollPane scroller=new JScrollPane(display);
viewport=scroller.getViewport();
viewport.setViewPosition(new Point(0,0));

I'm sure you're right with the 0,0 point position being the top left corner, so I don't think it's that.

Any other ideas about what I'm doing wrong?
Thanks again.
bd
 
I think you will need to either call
Code:
viewport.fireStateChange(); // most likely, or
viewport.firePropertyChange(); // not likely

to notify the UI that you have changed the viewport so it can update itself.


-------------------------------------------
There are no onions, only magic
-------------------------------------------
 
Thanks a lot for this, I'm definitely getting nearer. I really apprectiate your help.
I'm not sure how to call fireStateChanged(), it has protected access in JViewPort?

I have also tried adding a ChangeListener to the JViewPort and set the view position there like this

viewport.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
viewport.setViewPosition(new Point(0,0));
}
});

And although it worked it was stuck at the top!!

Following your advice I've been trying to set the viewport position in an actionPerformed method for a button that prints data to the JEditorPane, is that right?
I'm sure you're right in that if I could call the fireStateChanged method after setting the position it would set it to the correct position, I'm just not sure how to call it. Any more ideas??
Thanks again.
bd
 
You can also do it by placing the cursor (the caret) at the top(/bottom/where ever) of the document.
 
Yea, I had a problem with this a while back, though i was trying to get it to scroll, not to stop, either way. it goes something like this:
Caret c = yourtextarea.getCaret();
c.setDot(0);
 
Thanks for this, I'll give it a try.
Cheers,
BD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top