JViewport programatically scrolling

Posted on 2005-05-16
Last Modified: 2012-05-05
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?

Question by:turtletimer
    LVL 92

    Expert Comment

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

    Author Comment

    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.
    LVL 92

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now