Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53647

How to adjust position of scroll in the scrollpane?

I have created JTextpane and inserted components like Jtextarea inside the JTextpane. The vertical scrollbar of the Jscrollpane of the JTextpane is automatically set to bottom when I insert new components in that JTextpane. But I want to keep it to be set to the top position. How can I do this?

Upvotes: 13

Views: 36879

Answers (6)

Jay Kumarr
Jay Kumarr

Reputation: 11

jScrollPane.getVerticalScrollBar().setValue(1);

Upvotes: 0

BIlly
BIlly

Reputation: 21

This works too:

JTextArea. myTextArea;

// ...

myTextArea.select(0, 0);   // force the scroll value to the top

Upvotes: 2

user9441203
user9441203

Reputation:

ScrollPane's scroll value is always between ( 0.0 - 1 )
for example

0.0 = 0%
0.1 = 10%
0.2 = 20%
0.25 = 25%
.... so on

And you can adjust the scroll position using these values. For example, in JavaFX

// suppose this is the scrollpane
ScrollPane pane = new ScrollPane();

// to scroll the scrollpane horizontally 10% from its current position
pane.setHvalue(pane.getHvalue() + 0.1);

// to scroll 100%
pane.setHvalue(1);

and so on...

apply logic as you need

Upvotes: -2

Jack Russet
Jack Russet

Reputation: 101

I have found that the easiest way to do this is the following:

public void scroll(int vertical) {        
    switch (vertical) {
        case SwingConstants.TOP:
            getVerticalScrollBar().setValue(0);
            break;
        case SwingConstants.CENTER:
            getVerticalScrollBar().setValue(getVerticalScrollBar().getMaximum());
            getVerticalScrollBar().setValue(getVerticalScrollBar().getValue() / 2);
            break;
        case SwingConstants.BOTTOM:  
            getVerticalScrollBar().setValue(getVerticalScrollBar().getMaximum());
            break;
    }
}

I placed this in an object which extended JScrollPane but you could also add the name of your JScrollPane before all the getVertivalScrollBar(). There is two setValue()s for CENTER because getMaximum() returns the bottom of the JScrollBar, not the lowest value it goes to. This also works for Horizontal Scrolling using getHorizontalScrollBar() in place of getverticalScrollBar().

Upvotes: 10

trashgod
trashgod

Reputation: 205765

It should be possible to set the DefaultCaret update policy to NEVER_UPDATE. See the article Text Area Scrolling for other uses.

Upvotes: 2

Adamski
Adamski

Reputation: 54697

Here's a utility class I use. It can be used to scroll to the top, bottom, left, right or horizonatal / vertical center of a JScrollPane.

public final class ScrollUtil {
    public static final int NONE = 0, TOP = 1, VCENTER = 2, BOTTOM = 4, LEFT = 8, HCENTER = 16, RIGHT = 32;
    private static final int OFFSET = 100; // Required for hack (see below).

    private ScrollUtil() {
    }

    /**
     * Scroll to specified location.  e.g. <tt>scroll(component, BOTTOM);</tt>.
     *
     * @param c JComponent to scroll.
     * @param part Location to scroll to.  Should be a bit-wise OR of one or moe of the values:
     * NONE, TOP, VCENTER, BOTTOM, LEFT, HCENTER, RIGHT.
     */
    public static void scroll(JComponent c, int part) {
        scroll(c, part & (LEFT|HCENTER|RIGHT), part & (TOP|VCENTER|BOTTOM));
    }

    /**
     * Scroll to specified location.  e.g. <tt>scroll(component, LEFT, BOTTOM);</tt>.
     *
     * @param c JComponent to scroll.
     * @param horizontal Horizontal location.  Should take the value: LEFT, HCENTER or RIGHT.
     * @param vertical Vertical location.  Should take the value: TOP, VCENTER or BOTTOM.
     */
    public static void scroll(JComponent c, int horizontal, int vertical) {
        Rectangle visible = c.getVisibleRect();
        Rectangle bounds = c.getBounds();

        switch (vertical) {
            case TOP:     visible.y = 0; break;
            case VCENTER: visible.y = (bounds.height - visible.height) / 2; break;
            case BOTTOM:  visible.y = bounds.height - visible.height + OFFSET; break;
        }

        switch (horizontal) {
            case LEFT:    visible.x = 0; break;
            case HCENTER: visible.x = (bounds.width - visible.width) / 2; break;
            case RIGHT:   visible.x = bounds.width - visible.width + OFFSET; break;
        }

        // When scrolling to bottom or right of viewport, add an OFFSET value.
        // This is because without this certain components (e.g. JTable) would
        // not scroll right to the bottom (presumably the bounds calculation
        // doesn't take the table header into account.  It doesn't matter if
        // OFFSET is a huge value (e.g. 10000) - the scrollRectToVisible method
        // still works correctly.

        c.scrollRectToVisible(visible);
    }
}

Upvotes: 10

Related Questions