Link to home
Start Free TrialLog in
Avatar of turtletimer
turtletimer

asked on

JViewport programatically scrolling

I created my own scroll pane by extending JViewport.  This is a chat window, so it's size is dynamic.  Everything works great except it won't stay scrolled to the bottom, it is always on the very top.  When someone types I want it to scroll so you can see what was typed.  I have a  JTextPane within the viewport.  I use the setCaret method to position the caret to the bottom the the textpane.  I also do this.

 public void autoScroll(){

        if(this.getView().getHeight()>this.getHeight()){
            int max = getMaxYExtent();
            Point mpt = new Point(pt.x, max);
            this.setViewPosition(mpt);
        }

 private int getMaxYExtent(){
       
        return this.getView().getSize().height-this.getSize().height;
    }

This does work.  I can see it scroll but something resets it back.  I tried debuggin and checking what happened after this and it just goes to a bunch of my paint methods.  What resets the viewport to the top?

Avatar of Mick Barry
Mick Barry
Flag of Australia image

Why did you need to create your own viewport?
And what component is help in your viewport?
Avatar of turtletimer
turtletimer

ASKER

I wanted custum scroll buttons and no thumb or track.....i thought this would be the easiest way as opposed to messing with the UI.  Everything works great except it doesn't stay scrolled like a JScrollPane.  

I don't understand your other question.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial