Skip to content

Commit

Permalink
Clamp slider widget values.
Browse files Browse the repository at this point in the history
  • Loading branch information
tinevez committed May 21, 2024
1 parent aba2c74 commit a33ca27
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,20 @@ public SliderPanel( final String name, final BoundedValue model, final int spinn
setLayout( new BorderLayout( 10, 10 ) );
setPreferredSize( PANEL_SIZE );

slider = new JSlider( SwingConstants.HORIZONTAL, model.getRangeMin(), model.getRangeMax(), model.getCurrentValue() );
final int imin = model.getRangeMin();
final int imax = model.getRangeMax();
int ivalue = model.getCurrentValue();
ivalue = Math.max( imin, ivalue );
ivalue = Math.min( imax, ivalue );
slider = new JSlider( SwingConstants.HORIZONTAL, imin, imax, ivalue );

spinner = new JSpinner();
spinner.setModel( new SpinnerNumberModel( model.getCurrentValue(), model.getRangeMin(), model.getRangeMax(), spinnerStepSize ) );
final double min = model.getRangeMin();
final double max = model.getRangeMax();
double value = model.getCurrentValue();
value = Math.min( max, value );
value = Math.max( min, value );
spinner.setModel( new SpinnerNumberModel( value, min, max, spinnerStepSize ) );

slider.addChangeListener( new ChangeListener()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,20 @@ public SliderPanelDouble(
setLayout( new BorderLayout( 10, 10 ) );
setPreferredSize( SliderPanel.PANEL_SIZE );

dmin = model.getRangeMin();
dmax = model.getRangeMax();
final int imin = 0;
final int imax = sliderLength;
int ivalue = toSlider( model.getCurrentValue() );
ivalue = Math.max( imin, ivalue );
ivalue = Math.min( imax, ivalue );
slider = new JSlider( SwingConstants.HORIZONTAL, imin, imax, ivalue );

slider = new JSlider( SwingConstants.HORIZONTAL, 0, sliderLength, toSlider( model.getCurrentValue() ) );
spinner = new JSpinner();
spinner.setModel( new SpinnerNumberModel( model.getCurrentValue(), dmin, dmax, spinnerStepSize ) );
dmin = model.getRangeMin();
dmax = model.getRangeMax();
double value = model.getCurrentValue();
value = Math.min( dmax, value );
value = Math.max( dmin, value );
spinner.setModel( new SpinnerNumberModel( value, dmin, dmax, spinnerStepSize ) );

slider.addChangeListener( new ChangeListener()
{
Expand Down

0 comments on commit a33ca27

Please sign in to comment.