JavaFX Integer Spinner (IntegerSpinnerValueFactory) does not wrap around the value to minimum

亡梦爱人 提交于 2019-12-05 13:37:25

This is a known bug: JDK-8193286. The submitter included a workaround—subclassing IntegerSpinnerValueFactory:

package sample; 

import javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory; 

public final class IntSpinnerValueFactory extends IntegerSpinnerValueFactory { 

  public IntSpinnerValueFactory(final int min, final int max) { 
    super(min, max); 
  } 

  public IntSpinnerValueFactory(final int min, final int max, final int initialValue) { 
    super(min, max, initialValue, 1); 
  } 

  @Override 
  public void increment(final int steps) { 
    final int min = getMin(); 
    final int max = getMax(); 
    final int currentValue = getValue(); 
    final int newIndex = currentValue + steps * getAmountToStepBy(); 
    setValue(newIndex <= max ? newIndex : (isWrapAround() ? (newIndex - min) % (max - min + 1) + min : max)); 
  } 

} 

Note: Workaround has been slightly modified based on recommendations.

I have similar problem with IntegerSpinner - but wrapping from -11 to 12 (negative to positive) - on reaching -11 or 12 it returns to zero. Solution was to make List spinner with Strings. So that using or setting the values I've needed to convert string to integer and vc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!