为什么我的表中显示了StringProperty [value:“”]

时间:2015-07-31 11:12:29

标签: java javafx javafx-2 javafx-8

我正试图在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;
    }

}

我正在尝试在表格中添加一些行。这是我的全部代码:

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();

    }

}

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

enter image description here

我在这里缺少什么?我知道它很小,但作为一个新手,我无法发现它。 你能帮我推吗?

1 个答案:

答案 0 :(得分:6)

您在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 Property 。另请参阅this Q&A entry