【发布时间】:2012-11-12 22:59:04
【问题描述】:
我有一个 javafx 可编辑的组合框。
value 属性仅在我按 enter 时更新,而不仅仅是退出 ComboBox。
在我看来,这似乎是一个错误而不是设计功能,因为如果一个非聚焦控件显示一个未由其 value 属性反映的值,那就太奇怪了。
这是一个显示问题的 SSCE:
import javafx.application.Application;
import javafx.beans.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class T02 extends Application {
public static void main (String [] args) { launch(args); }
@Override public void start(Stage stage) throws Exception {
System.out.println("javafx.runtime.version: " + System.getProperties().get("javafx.runtime.version"));
VBox vbox = new VBox();
//this combobox is the object of the investigation
final ComboBox comboBox = new ComboBox();
comboBox.setEditable(true);
vbox.getChildren().add(comboBox);
//I need another component just to allow the ComboBox to loose the focus
TextField textField = new TextField();
vbox.getChildren().add(textField);
//And here it is: when comboBox looses focus, i print its state
comboBox.focusedProperty().addListener(new InvalidationListener() {
@Override public void invalidated(Observable observable) {
//return if it has the focus: i am just interested on focus lost
if (comboBox.focusedProperty().get()) {return;}
System.out.println(comboBox.getValue());
}
} );
stage.setScene(new Scene(vbox));
stage.show();
}
}
输出:
[在combo上写点东西,然后点击另一个控件(或者按tab,都是一样的)]
空
[单击组合键,按回车键并单击文本字段]
你好
第一个 null 很奇怪,可能是我之前所说的错误。我对 jira 进行了快速搜索,但没有发现这种行为。 (可能我没注意到)
更新
好的,我刚刚添加了
System.out.println("javafx.runtime.version: " + System.getProperties().get("javafx.runtime.version"));
并得到结果:
javafx.runtime.version: 2.1.something
我已经升级了最后一个可用的版本:
javafx.runtime.version: 2.2.3-b05
问题就消失了。
【问题讨论】: