【问题标题】:Can't make "Select All" CheckBox for TableView work (JavaFX)无法使 TableView 的“全选”复选框工作(JavaFX)
【发布时间】:2016-01-06 02:33:34
【问题描述】:

这是我当前的代码(从我的项目复制到它自己的“程序”中)

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.stage.Stage;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class Table extends Application {

    private static ObservableList<Item> items = FXCollections.observableArrayList();

        public static void updateItems() {

        }
        public static void initTable(Stage stage) {
            //Create table and columns
            TableView<Item> table_items = new TableView<>();
            TableColumn<Item, Boolean> column_selected = new TableColumn<>();
            TableColumn<Item, String> column_name = new TableColumn<>("Name");
            TableColumn<Item, Integer> column_quantity = new TableColumn<>("Quantity");
            TableColumn<Item, Double> column_price = new TableColumn<>("Price");
            //Create checkbox
            CheckBox select_all = new CheckBox();
            //Create scene
            Scene scene = new Scene(table_items);

            //Add items to ObservableList
            items.addAll(new Item("Knife", 10, 20),
                    new Item("Fork", 10, 25),
                    new Item("Pork", 3, 100));

            //Make table editable
            table_items.setEditable(true);

            //Make one column use checkboxes instead of text
            column_selected.setCellFactory(CheckBoxTableCell.forTableColumn(column_selected));

            //Change ValueFactory for each column
            column_selected.setCellValueFactory(new PropertyValueFactory<>("selected"));
            column_price.setCellValueFactory(new PropertyValueFactory<>("averagePrice"));
            column_quantity.setCellValueFactory(new PropertyValueFactory<>("volume"));
            column_name.setCellValueFactory(new PropertyValueFactory<>("name"));

            //Use box as column header
            column_selected.setGraphic(select_all);

            //Select all checkboxes when checkbox in header is pressed
            select_all.setOnAction(e -> selectAllBoxes(e));

            //Add columns to the table
            table_items.getColumns().addAll(column_selected, column_name, column_quantity, column_price);

            table_items.setItems(items);
            stage.setScene(scene);


        }

        public static void selectAllBoxes(ActionEvent e) {

            //Iterate through all items in ObservableList
            for (Item item : items) {
                //And change "selected" boolean
                item.setSelected(((CheckBox) e.getSource()).isSelected());
            }

        }

    @Override
    public void start(Stage primaryStage) throws Exception {
        initTable(primaryStage);
        primaryStage.show();
    }

    public static class Item {
            public BooleanProperty selected = new SimpleBooleanProperty(false);
            final private String name;
            final private double averagePrice;
            final private int volume;

            Item(String name, double averagePrice, int volume) {

                this.name = name;
                this.averagePrice = averagePrice;
                this.volume = volume;
            }

            public BooleanProperty isChecked() {
                return selected;
            }

            public void setSelected(boolean selected) {
                this.selected.set(selected);
            }

            public String getName() {
                return name;
            }

            public double getAveragePrice() {
                return averagePrice;
            }

            public int getVolume() {
                return volume;
            }

        }
}

它确实改变了列表中的selected BooleanProperty,但表中的复选框不会更新。起初我使用原始布尔值,它不起作用。然后我尝试了 BooleanProperty ,但它仍然不起作用(我希望什么?)。我认为只使用TableView#setItems 就可以解决问题,但它肯定不会。

【问题讨论】:

    标签: java user-interface checkbox javafx tableview


    【解决方案1】:

    您的模型类 Item 的定义方式与 PropertyValueFactory 不兼容(文档解释了它需要的结构)。

    所以你需要有

    public static class Item {
            private BooleanProperty selected = new SimpleBooleanProperty(false);
            final private String name;
            final private double averagePrice;
            final private int volume;
    
            Item(String name, double averagePrice, int volume) {
    
                this.name = name;
                this.averagePrice = averagePrice;
                this.volume = volume;
            }
    
            public BooleanProperty selectedProperty() {
                return selected;
            }
    
            public void setSelected(boolean selected) {
                this.selected.set(selected);
            }
    
            public boolean isSelected() {
                return selected.get();
            }
    
            public String getName() {
                return name;
            }
    
            public double getAveragePrice() {
                return averagePrice;
            }
    
            public int getVolume() {
                return volume;
            }
    
    }
    

    这将允许CheckBoxTableCell 正确观察其项目的selectedProperty(),因此它会在属性更改时更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 2019-04-21
      • 2014-06-08
      • 1970-01-01
      • 2018-04-06
      • 2015-11-21
      相关资源
      最近更新 更多