可编辑的Combobox不会保持其价值

时间:2015-06-17 12:35:16

标签: combobox javafx javafx-8

使用可编辑的组合框时出现问题 当我从下拉菜单中选择一个值时,它必须在组合框中显示,并且必须写入一个额外的文本字段,只要组合框被聚焦,它就会起作用。

这里的问题是,当我通过按Enter或tab将焦点设置在另一个元素上以及单击另一个元素时,textfield和组合框中的值就会消失。

我很感激任何帮助。

1 个答案:

答案 0 :(得分:0)

我发布了一个答案来详细说明我的评论(由Zydar提出)

使用JavaFX,您可以相互绑定一些属性以同步它们的值。这是一个使用它的完美案例。它应该是这样的骨头:

package test;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;


public class JavaFxTest extends Application {


    public void start(Stage primaryStage) throws Exception {

        ComboBox<String> cb = new ComboBox<>(FXCollections.observableArrayList("value1", "value2", "value3"));
        cb.setEditable(true);

        TextField tf = new TextField();
        tf.textProperty().bind(cb.getEditor().textProperty());
        // Use the following if you want the bind to be bidirectional
        // tf.textProperty().bindBidirectional(cb.getEditor().textProperty());

        FlowPane flowPane = new FlowPane(cb, tf);
        Scene scene = new Scene(flowPane);
        primaryStage.setScene(scene);
        primaryStage.show();

    }


    public static void main(String[] args) {

        JavaFxTest.launch(args);

    }
}
相关问题