[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 435
  • Last Modified:

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(){

            int max = getMaxYExtent();
            Point mpt = new Point(pt.x, max);

 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?

  • 2
1 Solution
Why did you need to create your own viewport?
And what component is help in your viewport?
turtletimerAuthor Commented:
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.
The scoll bars are actually a part of the JScrollPane, and not the JViewport.
If you don't want them then you should be able to use the JViewport directly.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now