【问题标题】:javafx-2, editable ComboBox value set only on enter keyjavafx-2,仅在输入键上设置的可编辑组合框值
【发布时间】: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

问题就消失了。

【问题讨论】:

    标签: combobox javafx-2


    【解决方案1】:

    RT-21454 ComboBox value should update when focus leaves it中修复的相关bug。

    更新

    AgostinoX 报告说升级到JavaFX 2.2.3-b05 解决了这个问题。

    您可以通过将以下代码放入 JavaFX 应用程序的启动方法中来检查您正在使用的 javafx 版本。

    System.out.println("javafx.runtime.version: " + System.getProperties().get("javafx.runtime.version"));
    

    【讨论】:

    • 你的回答引导我找到解决方案,也许你可以为阅读问题的人附加一个明确的“检查你的 javafx 版本”建议。
    • 我根据您的建议和问题更新中的版本检查代码澄清了答案。
    猜你喜欢
    • 2014-07-21
    • 2012-12-03
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 2021-01-31
    相关资源
    最近更新 更多