Java JTree scrollToSelection(JTree tree)

Here you can find the source of scrollToSelection(JTree tree)

Description

scroll To Selection

License

Open Source License

Declaration

public static void scrollToSelection(JTree tree) 

Method Source Code

//package com.java2s;
/*/*from  w w w .  j a  v  a2  s  .  c  o m*/
 *     GENESE - Gerador de N?meros e Estat?sticas para Mega-Sena
 *     Copyright (C)  2016  Rafael Teixeira
 *     rafaelfst@live.com
 *
 *     GENESE ? um software livre: voc? pode redistribu?-lo e/ou modific?-lo
 *     dentro dos termos da Licen?a P?blica Geral GNU como publicada pela
 *     Funda??o do Software Livre (FSF), na vers?o 3 da Licen?a, ou
 *     (na sua opini?o) qualquer vers?o posterior.
 *
 *     Este programa ? distribu?do na esperan?a de que possa ser ?til,
 *     mas SEM NENHUMA GARANTIA; sem uma garantia impl?cita de ADEQUA??O
 *     a qualquer MERCADO ou APLICA??O EM PARTICULAR. Veja a
 *     Licen?a P?blica Geral GNU para maiores detalhes.
 *
 *     Voc? deve ter recebido uma c?pia da Licen?a P?blica Geral GNU junto
 *     com este programa. Se n?o, veja <http://www.gnu.org/licenses/>.
 */

import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.JTree;
import javax.swing.JViewport;

public class Main {
    public static void scrollToSelection(JTree tree) {
        if (!(tree.getParent() instanceof JViewport))
            return;

        setViewPortPosition((JViewport) tree.getParent(), tree.getPathBounds(tree.getSelectionPath()));

    }

    private static void setViewPortPosition(JViewport viewport, Rectangle position) {
        // The location of the viewport relative to the object
        Point pt = viewport.getViewPosition();

        // Translate the cell location so that it is relative
        // to the view, assuming the northwest corner of the
        // view is (0,0)
        position.setLocation(position.x - pt.x, position.y - pt.y);

        // Scroll the area into view
        viewport.scrollRectToVisible(position);
    }
}

Related

  1. restoreExpanstionState(JTree tree, int row, String expansionState)
  2. revalidateTree(java.awt.Component c)
  3. saveExpansionState(JTree tree)
  4. saveExpansionStateStrings(JTree tree)
  5. saveTreeSelection(JTree tree)
  6. scrollTreeToSelectedRow(final JTree tree)
  7. setComponentTreeEnabled(Component c, boolean enabled)
  8. setLeftChildIndent(JTree tree, int indent)
  9. setToolTipOnTree(Container c, String tooltip)