Java Code Examples for javax.swing.JScrollBar#getUnitIncrement()

The following examples show how to use javax.swing.JScrollBar#getUnitIncrement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChartPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void scroll(JScrollBar scrollBar, MouseWheelEvent e) {
    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        int unitsToScroll = e.getUnitsToScroll();
        int direction = unitsToScroll < 0 ? -1 : 1;
        if (unitsToScroll != 0) {
            int increment = scrollBar.getUnitIncrement(direction);
            int oldValue = scrollBar.getValue();
            int newValue = oldValue + increment * unitsToScroll;
            newValue = Math.max(Math.min(newValue, scrollBar.getMaximum() -
                    scrollBar.getVisibleAmount()), scrollBar.getMinimum());
            if (oldValue != newValue) scrollBar.setValue(newValue);
        }
    }
}
 
Example 2
Source File: ProfilerTableContainer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void scroll(JScrollBar scroller, MouseWheelEvent event) {
    if (event.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        int direction = event.getUnitsToScroll() < 0 ? -1 : 1;
        int increment = scroller.getUnitIncrement(direction);
        int amount = event.getScrollAmount();
        int oldValue = scroller.getValue();
        int newValue = oldValue + increment * amount * direction;
        if (oldValue != newValue) scroller.setValue(newValue);
        event.consume();
    }
}
 
Example 3
Source File: DebuggingViewComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void adjustTreeScrollBar(int treeViewWidth) {
    DebugTreeView tView = getTreeView();
    if (tView == null) {
        scrollBarPanel.setVisible(false);
        return;
    }
    JViewport viewport = tView.getViewport();
    Point point = viewport.getViewPosition();
    if (point.y < 0) {
        viewport.setViewPosition(new Point(point.x, 0));
    }
    Dimension viewSize = viewport.getExtentSize();
    Dimension treeSize = viewport.getViewSize();
    if (treeViewWidth < 0) {
        treeViewWidth = treeSize.width;
    }
    int unitHeight = tView.getUnitHeight();
    if (unitHeight > 0) {
        JScrollBar sbar = mainScrollPane.getVerticalScrollBar();
        if (sbar.getUnitIncrement() != unitHeight) {
            sbar.setUnitIncrement(unitHeight);
        }
    }
    if (treeViewWidth <= viewSize.width) {
        scrollBarPanel.setVisible(false);
    } else {
        treeScrollBar.setMaximum(treeViewWidth);
        treeScrollBar.setVisibleAmount(viewSize.width);
        if (unitHeight > 0) {
            treeScrollBar.setUnitIncrement(unitHeight / 2);
        }
        treeScrollBar.setBlockIncrement(viewSize.width);
        scrollBarPanel.setVisible(true);
    } // else
}
 
Example 4
Source File: DebuggingViewComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
    JScrollBar scrollBar = mainScrollPane.getVerticalScrollBar();
    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        int totalScrollAmount = (int) (e.getPreciseWheelRotation() * e.getScrollAmount() * scrollBar.getUnitIncrement());
        scrollBar.setValue(scrollBar.getValue() + totalScrollAmount);
    }
}
 
Example 5
Source File: JavaOverviewSummary.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private static void scroll(JScrollBar scroller, MouseWheelEvent event) {
            if (event.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
                int direction = event.getUnitsToScroll() < 0 ? -1 : 1;
                int increment = scroller.getUnitIncrement(direction);
//                int amount = event.getScrollAmount();
                int amount = 1;
                int oldValue = scroller.getValue();
                int newValue = oldValue + increment * amount * direction;
                if (oldValue != newValue) scroller.setValue(newValue);
                event.consume();
            }
        }
 
Example 6
Source File: ChartPanel.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private static void scroll(JScrollBar scrollBar, MouseWheelEvent e) {
    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        int unitsToScroll = e.getUnitsToScroll();
        int direction = unitsToScroll < 0 ? -1 : 1;
        if (unitsToScroll != 0) {
            int increment = scrollBar.getUnitIncrement(direction);
            int oldValue = scrollBar.getValue();
            int newValue = oldValue + increment * unitsToScroll;
            newValue = Math.max(Math.min(newValue, scrollBar.getMaximum() -
                    scrollBar.getVisibleAmount()), scrollBar.getMinimum());
            if (oldValue != newValue) scrollBar.setValue(newValue);
        }
    }
}
 
Example 7
Source File: ProfilerTableContainer.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private static void scroll(JScrollBar scroller, MouseWheelEvent event) {
    if (event.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        int direction = event.getUnitsToScroll() < 0 ? -1 : 1;
        int increment = scroller.getUnitIncrement(direction);
        int amount = event.getScrollAmount();
        int oldValue = scroller.getValue();
        int newValue = oldValue + increment * amount * direction;
        if (oldValue != newValue) scroller.setValue(newValue);
        event.consume();
    }
}
 
Example 8
Source File: ChartPanel.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private static void scroll(JScrollBar scrollBar, MouseWheelEvent e) {
    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        int unitsToScroll = e.getUnitsToScroll();
        int direction = unitsToScroll < 0 ? -1 : 1;
        if (unitsToScroll != 0) {
            int increment = scrollBar.getUnitIncrement(direction);
            int oldValue = scrollBar.getValue();
            int newValue = oldValue + increment * unitsToScroll;
            newValue = Math.max(Math.min(newValue, scrollBar.getMaximum() -
                    scrollBar.getVisibleAmount()), scrollBar.getMinimum());
            if (oldValue != newValue) scrollBar.setValue(newValue);
        }
    }
}
 
Example 9
Source File: FlatScrollPaneUI.java    From FlatLaf with Apache License 2.0 4 votes vote down vote up
private void mouseWheelMovedSmooth( MouseWheelEvent e ) {
		// return if there is no viewport
		JViewport viewport = scrollpane.getViewport();
		if( viewport == null )
			return;

		// find scrollbar to scroll
		JScrollBar scrollbar = scrollpane.getVerticalScrollBar();
		if( scrollbar == null || !scrollbar.isVisible() || e.isShiftDown() ) {
			scrollbar = scrollpane.getHorizontalScrollBar();
			if( scrollbar == null || !scrollbar.isVisible() )
				return;
		}

		// consume event
		e.consume();

		// get precise wheel rotation
		double rotation = e.getPreciseWheelRotation();

		// get unit and block increment
		int unitIncrement;
		int blockIncrement;
		int orientation = scrollbar.getOrientation();
		Component view = viewport.getView();
		if( view instanceof Scrollable ) {
			Scrollable scrollable = (Scrollable) view;

			// Use (0, 0) view position to obtain constant unit increment of first item
			// (which might otherwise be variable on smaller-than-unit scrolling).
			Rectangle visibleRect = new Rectangle( viewport.getViewSize() );
			unitIncrement = scrollable.getScrollableUnitIncrement( visibleRect, orientation, 1 );
			blockIncrement = scrollable.getScrollableBlockIncrement( visibleRect, orientation, 1 );

			if( unitIncrement > 0 ) {
				// For the case that the first item (e.g. in a list) is larger
				// than the other items, get the unit increment of the second item
				// and use the smaller one.
				if( orientation == SwingConstants.VERTICAL ) {
					visibleRect.y += unitIncrement;
					visibleRect.height -= unitIncrement;
				} else {
					visibleRect.x += unitIncrement;
					visibleRect.width -= unitIncrement;
				}
				int unitIncrement2 = scrollable.getScrollableUnitIncrement( visibleRect, orientation, 1 );
				if( unitIncrement2 > 0 )
					unitIncrement = Math.min( unitIncrement, unitIncrement2 );
			}
		} else {
			int direction = rotation < 0 ? -1 : 1;
			unitIncrement = scrollbar.getUnitIncrement( direction );
			blockIncrement = scrollbar.getBlockIncrement( direction );
		}

		// limit scroll amount (number of units to scroll) for small viewports
		// (e.g. vertical scrolling in file chooser)
		int scrollAmount = e.getScrollAmount();
		int viewportWH = (orientation == SwingConstants.VERTICAL)
			? viewport.getHeight()
			: viewport.getWidth();
		if( unitIncrement * scrollAmount > viewportWH )
			scrollAmount = Math.max( viewportWH / unitIncrement, 1 );

		// compute relative delta
		double delta = rotation * scrollAmount * unitIncrement;
		boolean adjustDelta = Math.abs( rotation ) < (1.0 + EPSILON);
		double adjustedDelta = adjustDelta
			? Math.max( -blockIncrement, Math.min( delta, blockIncrement ) )
			: delta;

		// compute new value
		int value = scrollbar.getValue();
		double minDelta = scrollbar.getMinimum() - value;
		double maxDelta = scrollbar.getMaximum() - scrollbar.getModel().getExtent() - value;
		double boundedDelta = Math.max( minDelta, Math.min( adjustedDelta, maxDelta ) );
		int newValue = value + (int) Math.round( boundedDelta );

		// set new value
		if( newValue != value )
			scrollbar.setValue( newValue );

/*debug
		System.out.println( String.format( "%4d  %9f / %4d %4d / %12f %5s %12f / %4d %4d %4d / %12f %12f %12f / %4d",
			e.getWheelRotation(),
			e.getPreciseWheelRotation(),
			unitIncrement,
			blockIncrement,
			delta,
			adjustDelta,
			adjustedDelta,
			value,
			scrollbar.getMinimum(),
			scrollbar.getMaximum(),
			minDelta,
			maxDelta,
			boundedDelta,
			newValue ) );
*/
	}