JavaFX Integer Spinner (IntegerSpinnerValueFactory) 不会将值环绕到最小值

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

我创建了一个具有值的整数微调器

min (5), max (15) and initialValue (12)wrapAround (true).

一旦微调器在增量过程中达到 max (15) 值,而不是像 documentation 中所说的那样将值重置为 min (5),它被重置为值 [=15] =]

public final void setWrapAround​(boolean value)

Sets the value of the property wrapAround.

Property description:

The wrapAround property is used to specify whether the value factory should be circular. For example, should an integer-based value model increment from the maximum value back to the minimum value (and vice versa).

注意:递减正常工作,一旦达到 min (5) 值,Spinner 值自动设置为 max (15)

public class IntSpinnerTest extends Application
{
  @Override
  public void start(Stage stage) throws Exception
  {
    var spinner = new Spinner<Integer>();

    var factory = new SpinnerValueFactory.IntegerSpinnerValueFactory(5, 15, 12);
    factory.setWrapAround(true);

    spinner.setValueFactory(factory);

    stage.setScene(new Scene(new BorderPane(spinner), 400, 200));

    stage.setTitle("IntSpinnerTest");
    stage.centerOnScreen();
    stage.show();
  }

  public static void main(String[] args)
  {
    launch(args);
  }
}

这是一个已知错误:JDK-8193286。提交者提供了一个解决方法——子类化 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)); 
  } 

} 

注意:解决方法已根据建议稍作修改。

我对 IntegerSpinner 有类似的问题 - 但从 -11 到 12(从负到正)换行 - 在达到 -11 或 12 时 returns 到零。解决方案是用字符串制作列表微调器。因此,使用或设置我需要将字符串转换为整数和 vc.

的值