代码之家  ›  专栏  ›  技术社区  ›  Saravana Kumar M

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

  •  4
  • Saravana Kumar M  · 技术社区  · 8 年前

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

    checkListView.setItems(employeesObservableList);
    

    直到这里一切都很好。

    因为我绑定了employee对象,所以在列表视图中,每个复选框值都是我的EmployeeObject的toString()。我不希望toString()值在那里,而是希望显示雇员(eno)的其他属性。

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

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

    提前感谢!

    1 回复  |  直到 8 年前
        1
  •  11
  •   James_D    8 年前

    的列表单元格 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> 指定用于显示所述项的复选框的布尔属性; 检查列表视图 定义方法 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