带有自定义对象的 javafx CheckListView 以显示特定属性

标签 javafx java-8 javafx-2 javafx-8 controlsfx

我的应用程序中有controlsfx CheckListView。我正在显示我的自定义对象(例如:员工)。我已经创建了一个员工对象列表并将其包装在一个可观察的列表中。现在我将可观察列表设置为我的 CheckListView。

checkListView.setItems(employeesObservableList);

到这里一切正常。

由于我绑定(bind)了员工对象,因此在 ListView 中,每个复选框值都是我的员工对象的 toString()。我不希望那里的 toString() 值,而是希望显示员工 (eno) 的一些其他属性。

我在这里看不到 cellValueFactory,也不知道如何利用 cellFactory 来完成我的任务,因为 CheckListView 已经设置了自己的 cellFactory。

所以我的问题是我想要一个带有我选择的复选框值的 CheckListView。

提前致谢!

最佳答案

CheckListView 使用的列表单元格是标准 CheckBoxListCell 来自 javafx.scene.control.cell .所以你可以覆盖 cellFactory有类似的东西:

checkListView.setCellFactory(listView -> new CheckBoxListCell(checkListView::getItemBooleanProperty) {
    @Override
    public void updateItem(Employee employee, boolean empty) {
        super.updateItem(employee, empty);
        setText(employee == null ? "" : employee.getEno());
    }
});

请注意 CheckBoxListCell<T>有一个构造函数采用 Callback<T, BooleanProperty>为显示项目的复选框指定 bool 属性; CheckListView定义一个方法getItemBooleanProperty(T item)它准确地返回这个值,所以它可以使用方法引用直接传递给这里的构造函数。

这是一个SSCCE:
import org.controlsfx.control.CheckListView;

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.cell.CheckBoxListCell;
import javafx.stage.Stage;

public class CheckListViewTest extends Application {


    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Test");

        CheckListView<Employee> checkListView = new CheckListView<Employee>();

        ObservableList<Employee> oblist = FXCollections.observableArrayList();
        for (int i = 1; i <= 40; i++) {
            oblist.add(new Employee("Employee " + i, i));
        }
        checkListView.setItems(oblist);

        checkListView.setCellFactory(lv -> new CheckBoxListCell<Employee>(checkListView::getItemBooleanProperty) {
            @Override
            public void updateItem(Employee employee, boolean empty) {
                super.updateItem(employee, empty);
                setText(employee == null ? "" : String.format("Employee number: %04d", employee.getEno()));
            }
        });

        checkListView.getCheckModel().getCheckedIndices().addListener(new ListChangeListener<Integer>() {
            @Override
            public void onChanged(javafx.collections.ListChangeListener.Change<? extends Integer> c) {
                while (c.next()) {
                    if (c.wasAdded()) {
                        for (int i : c.getAddedSubList()) {
                            System.out.println(checkListView.getItems().get(i).getName() + " selected");
                        }
                    }
                    if (c.wasRemoved()) {
                        for (int i : c.getRemoved()) {
                            System.out.println(checkListView.getItems().get(i).getName() + " deselected");
                        }
                    }
                }
            }
        });


        Scene scene = new Scene(checkListView);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static class Employee {
        private final StringProperty name = new SimpleStringProperty();
        private final IntegerProperty eno = new SimpleIntegerProperty();
        public Employee(String name, int eno) {
            setName(name) ;
            setEno(eno);
        }
        public final StringProperty nameProperty() {
            return this.name;
        }

        public final String getName() {
            return this.nameProperty().get();
        }

        public final void setName(final String name) {
            this.nameProperty().set(name);
        }

        public final IntegerProperty enoProperty() {
            return this.eno;
        }

        public final int getEno() {
            return this.enoProperty().get();
        }

        public final void setEno(final int eno) {
            this.enoProperty().set(eno);
        }


    }

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

这导致

enter image description here

关于带有自定义对象的 javafx CheckListView 以显示特定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41222727/

相关文章:

JavaFX FXML-SceneBuilder-图像可调整大小

使用 Color.web 时 JavaFX 找不到符号

java - 使用 javafx 从 webview 获取打印

java - 如何像swing JTable一样设置和获取JavaFX Table的单元格值

JavaFX 表更新线程架构不匹配

java - netbeans 中的 Maven 插件错误

java - 如何在使用流读取文件时保留换行符 - java 8

使用map进行多次调用的Java流检查结果

java - Stream.collect 方法中的组合器如何在 java 8 中工作?

java - 如何在 JavaFX 中获得窄进度条?