编程语言
首页 > 编程语言> > JavaFX Integer Spinner(IntegerSpinnerValueFactory)不会将值包装到最小值

JavaFX Integer Spinner(IntegerSpinnerValueFactory)不会将值包装到最小值

作者:互联网

我创建了一个带有值的Integer Spinner

min(5),max(15)和initialValue(12)以及wrapAround(true).

一旦微调器在增量期间达到max(15)值,而不是像在documentation中所说的那样将值重置为min(5),它将被重置为值10(max(15) – min(5))

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)); 
  } 

} 

注意:根据建议略微修改了变通方法.

标签:java,javafx,java-11,javafx-11
来源: https://codeday.me/bug/20190701/1345831.html