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

The following examples show how to use javax.swing.JScrollBar#getMinimum() . 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: SeaGlassScrollPaneUI.java    From seaglass with Apache License 2.0 6 votes vote down vote up
void scrollByBlock(JScrollBar scrollbar, int direction) {
    // This method is called from BasicScrollPaneUI to implement wheel
    // scrolling, and also from scrollByBlock().
    int oldValue = scrollbar.getValue();
    int blockIncrement = scrollbar.getBlockIncrement(direction);
    int delta = blockIncrement * ((direction > 0) ? +1 : -1);
    int newValue = oldValue + delta;

    // Check for overflow.
    if (delta > 0 && newValue < oldValue) {
        newValue = scrollbar.getMaximum();
    } else if (delta < 0 && newValue > oldValue) {
        newValue = scrollbar.getMinimum();
    }

    scrollbar.setValue(newValue);
}
 
Example 2
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 ) );
*/
	}