Java JScrollPane scrollToVisible(Component comp, JScrollPane parent)

Here you can find the source of scrollToVisible(Component comp, JScrollPane parent)

Description

Scrolls a component to the view position of a scrollpane if necessary

License

Open Source License

Parameter

Parameter Description
comp a parameter

Declaration

public static void scrollToVisible(Component comp, JScrollPane parent) 

Method Source Code


//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.awt.Component;

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

import javax.swing.JScrollPane;

public class Main {
    /**/*from www  .  ja va 2 s.c  om*/
     * Scrolls a component to the view position of a scrollpane if necessary
     * @param comp
     */
    public static void scrollToVisible(Component comp, JScrollPane parent) {
        // todo: optimize this, so that scrolling only happens when necessary (omit flicker)
        /** @todo comp.getBounds().getLocation() =?= comp.getLocation() */
        Point p0 = parent.getViewport().getViewPosition();
        Point p1 = comp.getLocation();
        Rectangle r0 = parent.getViewport().getBounds();
        Rectangle r1 = comp.getBounds();

        if (p0.x > p1.x || p0.y > p1.y || (p0.x < p1.x && (p0.x + r0.width < p1.x + r1.width))
                || (p0.y < p1.y && (p0.y + r0.height < p1.y + r1.height)))
            parent.getViewport().setViewPosition(comp.getBounds().getLocation());
    }
}

Related

  1. scrolltoHomePosition(JScrollPane jScrollPane1)
  2. scrollToTop(final JScrollPane scroller, int delay)
  3. scrollToTop(final JScrollPane scrollPane)
  4. scrollToTop(JScrollPane scrollPane)
  5. scrollToTopLater(final JScrollPane scrollPane)
  6. setDefaultScrollbarUnits(JScrollPane scrollPane)
  7. setScrollpane(JScrollPane scrollpane)
  8. setScrollPaneSize(JScrollPane scrollPane, int rows, int columns)
  9. setUnitIncrement(JScrollPane scroller)