为什么 StringProperty[value: ""] 显示在我的 table

Why StringProperty[value: ""] is displayed in my table

我正在尝试在 TableView 中显示一些信息,但我遇到了一些我无法解决的问题。

这是我正在尝试做的事情:

我有一个 Room 对象:

public class Room {

    private SimpleStringProperty id;
    private SimpleStringProperty description;
    private SimpleStringProperty isForRepair;
    private SimpleStringProperty comment;

    public Room(String id, String description, String isForRepair, String comment) {
        this.id = new SimpleStringProperty(id);
        this.description = new SimpleStringProperty(description);
        this.isForRepair = new SimpleStringProperty(isForRepair);
        this.comment = new SimpleStringProperty(comment);
    }

    public SimpleStringProperty getId() {
        return id;
    }

    public void setId(SimpleStringProperty id) {
        this.id = id;
    }

    public SimpleStringProperty getDescription() {
        return description;
    }

    public void setDescription(SimpleStringProperty description) {
        this.description = description;
    }

    public SimpleStringProperty getIsForRepair() {
        return isForRepair;
    }

    public void setIsForRepair(SimpleStringProperty isForRepair) {
        this.isForRepair = isForRepair;
    }

    public SimpleStringProperty getComment() {
        return comment;
    }

    public void setComment(SimpleStringProperty comment) {
        this.comment = comment;
    }

}

我正在尝试向我的 table 添加一些行。这是我的全部代码:

public class RoomsManager {

    private TableView<Room> table = new TableView<>();

    public RoomsManager() {
    }

    public void show() {
        Stage roomsStage = new Stage();

        final ObservableList<Room> data = FXCollections.observableArrayList(
                new Room("1", "The small one", "no", "Good condition")
        );

        Scene scene = new Scene(new Group());
        roomsStage.setTitle("Table View Sample");
        roomsStage.setWidth(355);
        roomsStage.setHeight(500);

        final Label label = new Label("Rooms");
        label.setFont(new Font("Arial", 20));

        table.setEditable(true);

        TableColumn idCol = new TableColumn("ID");
        idCol.setMinWidth(100);
        idCol.setCellValueFactory(
                new PropertyValueFactory<Room, String>("id")
        );
        TableColumn descriptionCol = new TableColumn("Description");
        descriptionCol.setMinWidth(100);
        descriptionCol.setCellValueFactory(
                new PropertyValueFactory<Room, String>("description")
        );
        TableColumn isForRepairCol = new TableColumn("Is for repair");
        isForRepairCol.setMinWidth(100);
        isForRepairCol.setCellValueFactory(
                new PropertyValueFactory<Room, String>("isForRepair")
        );
        TableColumn commentCol = new TableColumn("Comment");
        commentCol.setMinWidth(100);
        commentCol.setCellValueFactory(
                new PropertyValueFactory<Room, String>("comment")
        );
        table.setItems(data);
        table.getColumns().addAll(idCol, descriptionCol, isForRepairCol, commentCol);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        roomsStage.setScene(scene);

        roomsStage.show();

    }

}

但是我的 table 看起来像这样:

我在这里错过了什么?我知道它很小,但作为新手,我无法发现它。 你能推一下吗?

您在 Room 数据模型中没有遵守 JavaFX 特定的访问器语法:

如果字段是像这样的 ObservableValue:

private StringProperty val = new SimpleStringProperty();

访问器应为:

public String getVal() {
    val.get();
}

public void setVal(String newVal) {
    val.set(newVal);
}

public StringProperty valProperty() {
    return val;
}

注意方法 val属性。另见 this Q&A entry.