JavaFX 2中的组合框键值对 [英] Combo-box key value pair in JavaFX 2

查看:119
本文介绍了JavaFX 2中的组合框键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习JavaFX 2.

现在我正在尝试构建一个示例应用程序。然后我陷入了组合框架。

我没有在JavaFX中找到对combobox的键值对的任何引用。

http://docs.oracle.com/javafx/2/api/index.html 没有说明关键值对。

I am just starting to learn JavaFX 2.
Now I am trying to build a sample application. Then I got stuck in combobox.
I did not find any reference to key value pair for combobox in JavaFX.
The combobox javadoc at http://docs.oracle.com/javafx/2/api/index.html does not say much about key value pair.

如何创建具有不同显示值和实际值的项目的组合框?

How can I create a combobox that has items which have different display value and actual value ?

推荐答案

您有两种方法:

1.只需覆盖datamodel类中的 toString()方法。示例:

You have 2 approaches:
1. Simply override the toString() method in your datamodel class. Example:

public class Demo extends Application {

    private final ObservableList<Employee> data =
            FXCollections.observableArrayList(
            new Employee("Azamat", 2200.15),
            new Employee("Veli", 1400.0),
            new Employee("Nurbek", 900.5));

    @Override
    public void start(Stage primaryStage) {

        ComboBox<Employee> combobox = new ComboBox<>(data);
        combobox.getSelectionModel().selectFirst(); // Select first as default
        combobox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Employee>() {

            @Override
            public void changed(ObservableValue<? extends Employee> arg0, Employee arg1, Employee arg2) {
                if (arg2 != null) {
                    System.out.println("Selected employee: " + arg2.getName());
                }
            }
        });
        StackPane root = new StackPane();
        root.getChildren().add(combobox);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    public static class Employee {
        private String name;
        private Double salary;

        @Override
        public String toString() {
            return name + " (sal:" + salary + ")";
        }

        public Employee(String name, Double salary) {
            this.name = name;
            this.salary = salary;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Double getSalary() {
            return salary;
        }

        public void setSalary(Double salary) {
            this.salary = salary;
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

注意覆盖Employee类的toString()。组合框的键(实际值)将是 Employee 实例,显示值为 employee.toString() case。

2.第二种方法是设置组合框的cellFactory属性。

Note the overriden toString() of the Employee class. The combobox's key (actual value) will be the Employee instance and the display value is employee.toString() in this case.
2. Second approach is to set cellFactory property of the combobox.

combobox.setCellFactory(new Callback<ListView<Employee>, ListCell<Employee>>() {

    @Override
    public ListCell<Employee> call(ListView<Employee> arg0) {
        return new ListCell<Employee>() {

            private final Button btn;
            {
                setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                btn = new Button();
            }

            @Override
            protected void updateItem(Employee item, boolean empty) {
                super.updateItem(item, empty);

                if (item == null || empty) {
                    setGraphic(null);
                } else {
                    btn.setStyle(item.getSalary() > 1000 ? "-fx-base:red" : "-fx-base: green");
                    btn.setText(item.getName() + "=" + item.getSalary());
                    setGraphic(btn);
                }
            }
        };
    }
});

这种方法可以更有效地控制单元格渲染。您不仅可以格式化显示值,还可以将任何节点(控件)包含到单元格中(在本例中为按钮)并添加一些查看逻辑(item.getSalary()?:)。实际值保持不变,即Employee实例。

This approach gives more powerful control over cell rendering. You can not just format display value but also include any node (control) into cell (button in this case) and add some viewing logic (item.getSalary()?"":"") too. The actual value remains the same ie Employee instance.

这篇关于JavaFX 2中的组合框键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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