Java JScrollPane scrollToTop(final JScrollPane scroller, int delay)

Here you can find the source of scrollToTop(final JScrollPane scroller, int delay)

Description

scroll To Top

License

Open Source License

Declaration

public static void scrollToTop(final JScrollPane scroller, int delay) 

Method Source Code

//package com.java2s;
/*/*from w  ww .j  a v a  2 s.c om*/
Storybook: Scene-based software for novelists and authors.
Copyright (C) 2008 - 2012 Martin Mustun
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.Point;

import javax.swing.JComponent;

import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;

public class Main {
    public static void scrollToTop(final JScrollPane scroller) {
        scrollToTop(scroller, 100);
    }

    public static void scrollToTop(final JScrollPane scroller, int delay) {
        /*
        Timer timer = new Timer(delay, new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
        // make sure the scroller is on top
        //             scroller.getVerticalScrollBar().setValue(0);
        //             scroller.getHorizontalScrollBar().setValue(0);
        JViewport viewport = scroller.getViewport();
        JComponent comp = (JComponent) viewport.getView();
        comp.scrollRectToVisible(new Rectangle(0, 0, 10, 10));
           }
        });
        timer.setRepeats(false);
        timer.start();
        */

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JViewport viewport = scroller.getViewport();
                JComponent comp = (JComponent) viewport.getView();
                if (comp instanceof JTextPane) {
                    JTextPane textPane = (JTextPane) comp;
                    textPane.setCaretPosition(0);
                } else {
                    viewport.setViewPosition(new Point(0, 0));
                }
            }
        });
    }
}

Related

  1. moveEnd(JScrollPane pane)
  2. newJScrollPane(String title, Component c)
  3. resetScrollPane(final JScrollPane scrollPane)
  4. scrollToBottom(final JScrollPane scrollPane)
  5. scrolltoHomePosition(JScrollPane jScrollPane1)
  6. scrollToTop(final JScrollPane scrollPane)
  7. scrollToTop(JScrollPane scrollPane)
  8. scrollToTopLater(final JScrollPane scrollPane)
  9. scrollToVisible(Component comp, JScrollPane parent)