mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-12 23:07:15 -06:00
ScrollBar: fixed smooth scrolling issues when continuously scrolling (issue #50)
This commit is contained in:
@@ -22,6 +22,7 @@ import java.awt.Dimension;
|
|||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.beans.PropertyChangeListener;
|
import java.beans.PropertyChangeListener;
|
||||||
@@ -205,6 +206,16 @@ public class FlatScrollBarUI
|
|||||||
oldStyleValues = null;
|
oldStyleValues = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected TrackListener createTrackListener() {
|
||||||
|
return new FlatTrackListener();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ScrollListener createScrollListener() {
|
||||||
|
return new FlatScrollListener();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected PropertyChangeListener createPropertyChangeListener() {
|
protected PropertyChangeListener createPropertyChangeListener() {
|
||||||
PropertyChangeListener superListener = super.createPropertyChangeListener();
|
PropertyChangeListener superListener = super.createPropertyChangeListener();
|
||||||
@@ -452,11 +463,13 @@ public class FlatScrollBarUI
|
|||||||
* and then animate scroll bar value from old value to new value.
|
* and then animate scroll bar value from old value to new value.
|
||||||
*/
|
*/
|
||||||
public void runAndSetValueAnimated( Runnable r ) {
|
public void runAndSetValueAnimated( Runnable r ) {
|
||||||
if( !isSmoothScrollingEnabled() ) {
|
if( inRunAndSetValueAnimated || !isSmoothScrollingEnabled() ) {
|
||||||
r.run();
|
r.run();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inRunAndSetValueAnimated = true;
|
||||||
|
|
||||||
if( animator != null )
|
if( animator != null )
|
||||||
animator.cancel();
|
animator.cancel();
|
||||||
|
|
||||||
@@ -470,11 +483,16 @@ public class FlatScrollBarUI
|
|||||||
r.run();
|
r.run();
|
||||||
|
|
||||||
int newValue = scrollbar.getValue();
|
int newValue = scrollbar.getValue();
|
||||||
scrollbar.setValue( oldValue );
|
if( newValue != oldValue ) {
|
||||||
|
scrollbar.setValue( oldValue );
|
||||||
|
|
||||||
setValueAnimated( newValue );
|
setValueAnimated( newValue );
|
||||||
|
}
|
||||||
|
|
||||||
|
inRunAndSetValueAnimated = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean inRunAndSetValueAnimated;
|
||||||
private Animator animator;
|
private Animator animator;
|
||||||
private int targetValue = Integer.MIN_VALUE;
|
private int targetValue = Integer.MIN_VALUE;
|
||||||
private int delta;
|
private int delta;
|
||||||
@@ -524,6 +542,32 @@ public class FlatScrollBarUI
|
|||||||
return UIManager.getBoolean( "ScrollPane.smoothScrolling" );
|
return UIManager.getBoolean( "ScrollPane.smoothScrolling" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---- class FlatTrackListener --------------------------------------------
|
||||||
|
|
||||||
|
protected class FlatTrackListener
|
||||||
|
extends TrackListener
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void mousePressed( MouseEvent e ) {
|
||||||
|
runAndSetValueAnimated( () -> {
|
||||||
|
super.mousePressed( e );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//---- class FlatScrollListener -------------------------------------------
|
||||||
|
|
||||||
|
protected class FlatScrollListener
|
||||||
|
extends ScrollListener
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed( ActionEvent e ) {
|
||||||
|
runAndSetValueAnimated( () -> {
|
||||||
|
super.actionPerformed( e );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//---- class ScrollBarHoverListener ---------------------------------------
|
//---- class ScrollBarHoverListener ---------------------------------------
|
||||||
|
|
||||||
// using static field to disabling hover for other scroll bars
|
// using static field to disabling hover for other scroll bars
|
||||||
|
|||||||
Reference in New Issue
Block a user