mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-11 06:27:13 -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.Insets;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
@@ -205,6 +206,16 @@ public class FlatScrollBarUI
|
||||
oldStyleValues = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TrackListener createTrackListener() {
|
||||
return new FlatTrackListener();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ScrollListener createScrollListener() {
|
||||
return new FlatScrollListener();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PropertyChangeListener createPropertyChangeListener() {
|
||||
PropertyChangeListener superListener = super.createPropertyChangeListener();
|
||||
@@ -452,11 +463,13 @@ public class FlatScrollBarUI
|
||||
* and then animate scroll bar value from old value to new value.
|
||||
*/
|
||||
public void runAndSetValueAnimated( Runnable r ) {
|
||||
if( !isSmoothScrollingEnabled() ) {
|
||||
if( inRunAndSetValueAnimated || !isSmoothScrollingEnabled() ) {
|
||||
r.run();
|
||||
return;
|
||||
}
|
||||
|
||||
inRunAndSetValueAnimated = true;
|
||||
|
||||
if( animator != null )
|
||||
animator.cancel();
|
||||
|
||||
@@ -470,11 +483,16 @@ public class FlatScrollBarUI
|
||||
r.run();
|
||||
|
||||
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 int targetValue = Integer.MIN_VALUE;
|
||||
private int delta;
|
||||
@@ -524,6 +542,32 @@ public class FlatScrollBarUI
|
||||
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 ---------------------------------------
|
||||
|
||||
// using static field to disabling hover for other scroll bars
|
||||
|
||||
Reference in New Issue
Block a user