我从8u40b17开始使用Spinner.
SpinnerValueFactory svf = new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 100);
Spinner sp = new Spinner();
sp.setValueFactory(svf);
sp.setEditable(true);
sp.setPrefWidth(80);
Run Code Online (Sandbox Code Playgroud)
我注意到当我从键盘输入一些值并且我增加了上限值时,预期的数字不是下一个.而不是这个,它是下一个默认值.我怎么解决这个问题?
例如:如果我有5作为默认值而我输入34,那么按下我希望得到35的上箭头实际得到6.
我对旋转器控件有同样的问题.您的错误已在此处记录:JDK-8094205
这是最后的评论:
Jonathan Giles添加了评论 - 2014年12月15日上午12:59
在我的仓库本地修复,一旦打开,将在本周推送到8u60回购.现在,在调用递增/递减时提交文本编辑器输入(尽管在焦点丢失时仍未提交该值).
单元测试:
Run Code Online (Sandbox Code Playgroud)javafx.scene.control.SpinnerTest.test_rt_39655_decrement() javafx.scene.control.SpinnerTest.test_rt_39655_increment()
变更集:http://hg.openjdk.java.net/openjfx/8u-dev/rt/rev/89ca7d3f699e
这是我对Autocommit微调器的看法.这个将自动提交工厂将接受的任何东西.
public class SpinnerAutoCommit<T> extends Spinner<T> {
public SpinnerAutoCommit() {
super();
addListenerKeyChange();
}
public SpinnerAutoCommit(int min, int max, int initialValue) {
super(min, max, initialValue);
addListenerKeyChange();
}
public SpinnerAutoCommit(int min, int max, int initialValue, int amountToStepBy) {
super(min, max, initialValue, amountToStepBy);
addListenerKeyChange();
}
public SpinnerAutoCommit(double min, double max, double initialValue) {
super(min, max, initialValue);
addListenerKeyChange();
}
public SpinnerAutoCommit(double min, double max, double initialValue, double amountToStepBy) {
super(min, max, initialValue, amountToStepBy);
addListenerKeyChange();
}
public SpinnerAutoCommit(ObservableList<T> items) {
super(items);
addListenerKeyChange();
}
public SpinnerAutoCommit(SpinnerValueFactory<T> valueFactory) {
super(valueFactory);
addListenerKeyChange();
}
private void addListenerKeyChange() {
getEditor().textProperty().addListener((observable, oldValue, newValue) -> {
commitEditorText();
});
}
private void commitEditorText() {
if (!isEditable()) return;
String text = getEditor().getText();
SpinnerValueFactory<T> valueFactory = getValueFactory();
if (valueFactory != null) {
StringConverter<T> converter = valueFactory.getConverter();
if (converter != null) {
T value = converter.fromString(text);
valueFactory.setValue(value);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Spinner根据设计,仅当用户通过操作处理程序按下 ENTER 键时才会提交控件文本字段中的更改:
getEditor().setOnAction(action -> {
String text = getEditor().getText();
SpinnerValueFactory<T> valueFactory = getValueFactory();
if (valueFactory != null) {
StringConverter<T> converter = valueFactory.getConverter();
if (converter != null) {
T value = converter.fromString(text);
valueFactory.setValue(value);
}
}
});
Run Code Online (Sandbox Code Playgroud)
请注意,如果无法转换键入的值,则会抛出NumberFormatException,并在文本字段中保留错误的值。
我们可以提供自己的实现,通过事件过滤器监听其他键,例如 TAB 键,同时在出现异常的情况下恢复最后一个有效值。
像这样的东西:
private final Spinner sp = new Spinner();
@Override
public void start(Stage primaryStage) {
SpinnerValueFactory svf = new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 100);
sp.setValueFactory(svf);
sp.setEditable(true);
sp.setPrefWidth(80);
// Commit on TAB
sp.addEventFilter(KeyEvent.ANY, e->{
if (sp.isEditable() && e.getCode().equals(KeyCode.TAB)) {
doCommit();
e.consume();
}
});
// Override Commit on ENTER
sp.getEditor().setOnAction(e->{
if(sp.isEditable()) {
doCommit();
e.consume();
}
});
Scene scene = new Scene(new StackPane(sp), 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
/*
Commit new value, checking conversion to integer,
restoring old valid value in case of exception
*/
private void doCommit(){
String text = sp.getEditor().getText();
SpinnerValueFactory<Integer> valueFactory = sp.getValueFactory();
if (valueFactory != null) {
StringConverter<Integer> converter = valueFactory.getConverter();
if (converter != null) {
try{
Integer value = converter.fromString(text);
valueFactory.setValue(value);
} catch(NumberFormatException nfe){
sp.getEditor().setText(converter.toString(valueFactory.getValue()));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)