Java Code Examples for javax.swing.JComponent.scrollRectToVisible()

The following are Jave code examples for showing how to use scrollRectToVisible() of the javax.swing.JComponent class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: OpenGL-Bullet-engine   File: ScrollUtil.java   View Source Code Vote up 5 votes
/**
 * 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);
}
 
Example 2
Project: incubator-netbeans   File: CenteredZoomAnimator.java   View Source Code Vote up 5 votes
@Override public void tick(double progress) {
    double nextZoom = progress >= 1.0 ? targetZoom :
        (sourceZoom + progress * (targetZoom - sourceZoom));

    Scene scene = getScene();
    JComponent view = scene.getView ();

    if (view != null) {
        Point viewLocation = view.getVisibleRect ().getLocation();
        Dimension viewSize = view.getVisibleRect ().getSize();
        Point oldCenter = scene.convertSceneToView (center);

        ((DependencyGraphScene)scene).setMyZoomFactor (nextZoom);
        scene.validate (); // HINT - forcing to change preferred size of the JComponent view

        Point newCenter = scene.convertSceneToView (center);
        Rectangle viewBounds = view.getVisibleRect();
        Point visibleCenter = new Point((int)viewBounds.getCenterX(), (int)viewBounds.getCenterY());
        newCenter.x += Math.round((newCenter.x - visibleCenter.x) * progress);
        newCenter.y += Math.round((newCenter.y - visibleCenter.y) * progress);

        view.scrollRectToVisible (new Rectangle (
                newCenter.x - oldCenter.x + viewLocation.x,
                newCenter.y - oldCenter.y + viewLocation.y,
                viewSize.width,
                viewSize.height
        ));
    } else {
        ((DependencyGraphScene)scene).setMyZoomFactor (nextZoom);
    }
}
 
Example 3
Project: incubator-netbeans   File: DebuggingViewComponent.java   View Source Code Vote up 5 votes
/**
 * Restore stored scroll position.
 */
private void restoreScrollPosition(boolean delayScrollWithMarkingDirtyRegion) {
    if (visibleTreePosition != null) {
        JTree tree = getJTree();
        if (tree != null) {
            int row = tree.getRowForPath(visibleTreePosition.getPath());
            if (row != -1) {
                Rectangle bounds = tree.getRowBounds(row);
                if (bounds != null) {
                    int scrollY = bounds.y - visibleTreePosition.getOffset();
                    JViewport viewport = mainScrollPane.getViewport();
                    Rectangle rect = viewport.getViewRect();
                    rect.y = scrollY;
                    if (!rect.isEmpty()) {
                        JComponent view = (JComponent) viewport.getView();
                        if (delayScrollWithMarkingDirtyRegion) {
                            RepaintManager.currentManager(viewport).addDirtyRegion(
                                    view,
                                    rect.x, rect.x, rect.width, rect.height);
                        }
                        ignoreScrollAdjustment = true;
                        try {
                            view.scrollRectToVisible(
                                    rect);
                        } finally {
                            ignoreScrollAdjustment = false;
                        }
                    }
                }
            }
        }
    }
}
 
Example 4
Project: MaxSim   File: ExtendedSatelliteComponent.java   View Source Code Vote up 5 votes
private void moveVisibleRect(Point center) {
    JComponent component = scene.getView();
    if (component == null) {
        return;
    }
    double zoomFactor = scene.getZoomFactor();
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;

    int cx = (int) ((double) (center.x - vx) / scale * zoomFactor);
    int cy = (int) ((double) (center.y - vy) / scale * zoomFactor);

    Rectangle visibleRect = component.getVisibleRect();
    visibleRect.x = cx - visibleRect.width / 2;
    visibleRect.y = cy - visibleRect.height / 2;
    component.scrollRectToVisible(visibleRect);

    this.repaint();
}
 
Example 5
Project: MaxSim   File: CustomizablePanAction.java   View Source Code Vote up 5 votes
private boolean pan (Widget widget, Point newLocation) {
    if (scrollPane == null  ||  scene != widget.getScene ())
        return false;
    newLocation = scene.convertSceneToView (widget.convertLocalToScene (newLocation));
    SwingUtilities.convertPointToScreen (newLocation, scene.getView ());
    JComponent view = scene.getView ();
    Rectangle rectangle = view.getVisibleRect ();
    rectangle.x += lastLocation.x - newLocation.x;
    rectangle.y += lastLocation.y - newLocation.y;
    view.scrollRectToVisible (rectangle);
    lastLocation = newLocation;
    return true;
}
 
Example 6
Project: MaxSim   File: ExtendedSatelliteComponent.java   View Source Code Vote up 5 votes
private void moveVisibleRect(Point center) {
    JComponent component = scene.getView();
    if (component == null) {
        return;
    }
    double zoomFactor = scene.getZoomFactor();
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;

    int cx = (int) ((double) (center.x - vx) / scale * zoomFactor);
    int cy = (int) ((double) (center.y - vy) / scale * zoomFactor);

    Rectangle visibleRect = component.getVisibleRect();
    visibleRect.x = cx - visibleRect.width / 2;
    visibleRect.y = cy - visibleRect.height / 2;
    component.scrollRectToVisible(visibleRect);

}
 
Example 7
Project: MaxSim   File: CustomizablePanWidgetAction.java   View Source Code Vote up 5 votes
private boolean pan (Widget widget, Point newLocation) {
    if (scrollPane == null  ||  scene != widget.getScene ())
        return false;
    newLocation = scene.convertSceneToView (widget.convertLocalToScene (newLocation));
    SwingUtilities.convertPointToScreen (newLocation, scene.getView ());
    JComponent view = scene.getView ();
    Rectangle rectangle = view.getVisibleRect ();
    rectangle.x += lastLocation.x - newLocation.x;
    rectangle.y += lastLocation.y - newLocation.y;
    view.scrollRectToVisible (rectangle);
    lastLocation = newLocation;
    return true;
}
 
Example 8
Project: MaxSim   File: ExtendedSatelliteComponent.java   View Source Code Vote up 5 votes
private void moveVisibleRect(Point center) {
    JComponent component = scene.getView();
    if (component == null) {
        return;
    }
    double zoomFactor = scene.getZoomFactor();
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;

    int cx = (int) ((double) (center.x - vx) / scale * zoomFactor);
    int cy = (int) ((double) (center.y - vy) / scale * zoomFactor);

    Rectangle visibleRect = component.getVisibleRect();
    visibleRect.x = cx - visibleRect.width / 2;
    visibleRect.y = cy - visibleRect.height / 2;
    component.scrollRectToVisible(visibleRect);

}
 
Example 9
Project: MaxSim   File: CfgScene.java   View Source Code Vote up 5 votes
private void centerSelection() {
    Point sceneCenter = null;
    Collection<CfgNode> nodes = this.selectedNodes;
    if (nodes.size() == 0) {
        nodes = this.getNodes();
    }

    for (CfgNode n : nodes) {
        if (sceneCenter == null) {
            sceneCenter = this.findWidget(n).getLocation();
            continue;
        }
        Point location = this.findWidget(n).getLocation();
        sceneCenter.x = (location.x + sceneCenter.x) / 2;
        sceneCenter.y = (location.y + sceneCenter.y) / 2;
    }

    JComponent view = this.getView();
    if (view != null) {
        Rectangle viewBounds = view.getVisibleRect();

        Point viewCenter = this.convertSceneToView(sceneCenter);

        view.scrollRectToVisible(new Rectangle(
                viewCenter.x - viewBounds.width / 2,
                viewCenter.y - viewBounds.height / 2,
                viewBounds.width,
                viewBounds.height));
    }
}
 
Example 10
Project: openjdk-jdk10   File: ExtendedSatelliteComponent.java   View Source Code Vote up 5 votes
private void moveVisibleRect(Point center) {
    JComponent component = scene.getView();
    if (component == null) {
        return;
    }
    double zoomFactor = scene.getZoomFactor();
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;

    int cx = (int) ((double) (center.x - vx) / scale * zoomFactor);
    int cy = (int) ((double) (center.y - vy) / scale * zoomFactor);

    Rectangle visibleRect = component.getVisibleRect();
    visibleRect.x = cx - visibleRect.width / 2;
    visibleRect.y = cy - visibleRect.height / 2;
    component.scrollRectToVisible(visibleRect);

    this.repaint();
}
 
Example 11
Project: openjdk-jdk10   File: CustomizablePanAction.java   View Source Code Vote up 5 votes
private boolean pan (Widget widget, Point newLocation) {
    if (scrollPane == null  ||  scene != widget.getScene ())
        return false;
    newLocation = scene.convertSceneToView (widget.convertLocalToScene (newLocation));
    SwingUtilities.convertPointToScreen (newLocation, scene.getView ());
    JComponent view = scene.getView ();
    Rectangle rectangle = view.getVisibleRect ();
    rectangle.x += lastLocation.x - newLocation.x;
    rectangle.y += lastLocation.y - newLocation.y;
    view.scrollRectToVisible (rectangle);
    lastLocation = newLocation;
    return true;
}
 
Example 12
Project: openjdk-jdk10   File: ExtendedSatelliteComponent.java   View Source Code Vote up 5 votes
private void moveVisibleRect(Point center) {
    JComponent component = scene.getView();
    if (component == null) {
        return;
    }
    double zoomFactor = scene.getZoomFactor();
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;

    int cx = (int) ((double) (center.x - vx) / scale * zoomFactor);
    int cy = (int) ((double) (center.y - vy) / scale * zoomFactor);

    Rectangle visibleRect = component.getVisibleRect();
    visibleRect.x = cx - visibleRect.width / 2;
    visibleRect.y = cy - visibleRect.height / 2;
    component.scrollRectToVisible(visibleRect);

}
 
Example 13
Project: incubator-netbeans   File: DragManager.java   View Source Code Vote up 4 votes
public void scroll(JComponent component) {
    component.scrollRectToVisible(currentDragRect);            
}
 
Example 14
Project: ramus   File: TableViewPreferenciesDialog.java   View Source Code Vote up 4 votes
@Override
public void focusGained(FocusEvent e) {
    JComponent c = (JComponent) e.getComponent();
    c.scrollRectToVisible(new Rectangle(0, 0, c.getWidth(), c
            .getHeight()));
}