Here you can find the source of scrollToVisible(Component comp, JScrollPane parent)
Parameter | Description |
---|---|
comp | a parameter |
public static void scrollToVisible(Component comp, JScrollPane parent)
//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()); } }