代码之家  ›  专栏  ›  技术社区  ›  MattT

添加rowfactory后不显示Javafx表视图

  •  1
  • MattT  · 技术社区  · 7 年前

    我正在尝试设置一个netbeans/javafx程序,该程序显示一个Pet对象列表,并为每一行提供一个onclick侦听器。

    我在fxml中有以下内容:

    我的表格。fxml

    <VBox alignment="CENTER" layoutX="11.0" layoutY="12.0" prefWidth="345.0">
        <children>
            <TableView id="Table1" fx:id="Table1" styleClass="mytable"/>
            <TableView id="Table2" fx:id="Table2" styleClass="mytable"/>
            <TableView id="Table3" fx:id="Table3" styleClass="mytable"/>
            <TableView id="Table4" fx:id="Table4" styleClass="mytable"/>
        </children>
    </VBox>
    

    我使用的是netbeans,所以我为加载FXML的视图设置了一个topComponent,然后加载一个控制器类。

    在我的控制器类中,我有以下代码向表中添加内容。目的是每个表都有3行,分别对应于所包含Pet对象的ID、名称和所有者。

    MyTables控制器。Java语言

    public class MyTablesController implements Initializable {
    
        @FXML
        private TableView<Pet> Table1;
        @FXML
        private TableView<Pet> Table2;
        @FXML
        private TableView<Pet> Table3;
        @FXML
        private TableView<Pet> Table4;
    
        private TableView<Pet>[] allTables;
    
        public MyTablesController() {
            allTables = new TableView[4];
            allTables[0] = Table1;
            allTables[1] = Table2;
            allTables[2] = Table3;
            allTables[3] = Table4;
        }
    
        @Override
        public void initialize(URL location, ResourceBundle resources) {
            for(TableView<Pet> table : allTables) {
                table.getColumns().add(createCol("ID", Pet::idProperty, 100));
                table.getColumns().add(createCol("Name", Pet::nameProperty, 140));
                table.getColumns().add(createCol("Owner", Pet::ownerProperty, 100));
                table.getItems().addAll(
                        new Pet("1234", "spot", "joe"),
                        new Pet("5432", "snowball", "bill"),
                        new Pet("7612", "snoopy", "frank")
                );
            }
    
            setupTableRowDragEvents();
        }
    
        private TableColumn<Pet, String> createCol(String title, 
                Function<Pet, ObservableValue<String>> mapper, double size) {
    
            TableColumn<Pet, String> col = new TableColumn<>(title);
            col.setCellValueFactory(cellData -> mapper.apply(cellData.getValue()));
            col.setPrefWidth(size);
            return col ;
        }
    
        private void setupTableRowDragEvents() {
            for (TableView<Pet> table : allTables) {
                table.setRowFactory(tv -> {
                    TableRow<Pet> row = new TableRow<>();
                    row.setOnMouseClicked(event -> {
                        System.out.println("I CLICKED ON " + row.getItem().getName()");
                    });
                    return row;
                });
            }
        }
    }
    

    最后,这里是宠物对象:

    宠物Java语言

    public final class Pet {
    
        public final StringProperty id = new SimpleStringProperty(this, "id");
        public final StringProperty name = new SimpleStringProperty(this, "name");
        public final StringProperty owner = new SimpleStringProperty(this, "owner");
    
        public Pet() {}
    
        public Pet(String id, String name, String owner) {
            this.id.set(id);
            this.name.set(name);
            this.owner.set(owner);
        }
    
        public String getId() {
            return id.get();
        }
    
        public void setId(String id) {
            this.id.set(id);
        }
    
        public StringProperty idProperty() {
            return id;
        }
    
        public String getName() {
            return name.get();
        }
    
        public void setName(String name) {
            this.name.set(name);
        }
    
        public StringProperty nameProperty() {
            return name;
        }
    
        public String getOwner() {
            return owner.get();
        }
    
        public void setOwner(String owner) {
            this.owner.set(owner);
        }
    
        public StringProperty ownerProperty() {
            return owner;
        }
    }
    

    这是我的问题。

    如果我离开我的表控制器。初始化为空,4个图形显示正常。只有当我添加其余的代码(setCols调用和setupTableRowDrageEvents()调用)时。。。

    若initialize()中存在这两个元素中的任何一个或两个,则不会显示这些图。我没有收到任何错误消息或奇怪的行为,除了窗口完全为空,图形没有显示。

    我的代码基于 sort tableview with drag and drop (rows) . 与此相比,我不知道我在哪里搞砸了。这个答案是不是错了,没有人费心去检查?

    我做错了什么?

    1 回复  |  直到 7 年前
        1
  •  1
  •   James_D    7 年前

    在填充数组时,表视图引用都是 null ,因此用空引用填充数组。然后,当您遍历该数组时,第一次调用时将出现空指针异常 table.getColumns() . 将代码从控制器构造函数移到 initialize() :

    public class MyTablesController implements Initializable {
    
        @FXML
        private TableView<Pet> Table1;
        @FXML
        private TableView<Pet> Table2;
        @FXML
        private TableView<Pet> Table3;
        @FXML
        private TableView<Pet> Table4;
    
        private TableView<Pet>[] allTables;
    
        @Override
        public void initialize(URL location, ResourceBundle resources) {
    
            allTables = new TableView[4];
            allTables[0] = Table1;
            allTables[1] = Table2;
            allTables[2] = Table3;
            allTables[3] = Table4;
    
            for(TableView<Pet> table : allTables) {
                table.getColumns().add(createCol("ID", Pet::idProperty, 100));
                table.getColumns().add(createCol("Name", Pet::nameProperty, 140));
                table.getColumns().add(createCol("Owner", Pet::ownerProperty, 100));
                table.getItems().addAll(
                        new Pet("1234", "spot", "joe"),
                        new Pet("5432", "snowball", "bill"),
                        new Pet("7612", "snoopy", "frank")
                );
            }
    
            setupTableRowDragEvents();
        }
    
        // ...
    
    }