JavaFX Integer Spinner(IntegerSpinnerValueFactory)不会将值包装到最小值 [英] JavaFX Integer Spinner (IntegerSpinnerValueFactory) does not wrap around the value to minimum

查看:292
本文介绍了JavaFX Integer Spinner(IntegerSpinnerValueFactory)不会将值包装到最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个整数微调器,其值为

I have created an Integer Spinner with values

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

一旦微调器到达 max( 15)增量期间的值,而不是将值重置为 min(5),如文档,它被重置为值 10(最大(15) - 分钟(5))

Once the spinner reaches the max (15) value during increment, instead of resetting the value to min (5) as it says in the documentation, it is being reset to value 10 (max (15) - min (5))


public final void setWrapAround(boolean值)

public final void setWrapAround​(boolean value)

设置属性wrapAround的值。

Sets the value of the property wrapAround.

属性说明:

wrapAround属性用于指定值factory是否应为循环。例如,基于整数的值模型应该从最大值增加回到最小值(反之亦然)。

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)

Note: Decrement works properly, once it reaches the min (5) value, Spinner value automatically set to 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

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

} 

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

这篇关于JavaFX Integer Spinner(IntegerSpinnerValueFactory)不会将值包装到最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆