Why StringProperty[value: “”] is displayed in my t

2019-02-26 02:34发布

I'm trying to show some information in a TableView, but I'm facing some issues that I can't fix.

Here is what I'm trying to do:

I have a Room object:

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;
    }

}

And I'm trying to add some rows to my table. Here is my whole code:

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

    }

}

But my table looks like this:

enter image description here

What am I missing here? I know that it is something small, but as a newbiew, I'm not able to spot it. Can yuo give me a push?

1条回答
贪生不怕死
2楼-- · 2019-02-26 03:18

You didn't obey JavaFX specific accessor syntax in your Room datamodel:

If the field is ObservableValue like:

private StringProperty val = new SimpleStringProperty();

There should be accessors as:

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

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

public StringProperty valProperty() {
    return val;
}

Note the method valProperty. Also see this Q&A entry.

查看更多
登录 后发表回答