gpt4 book ai didi

java - 为什么 StringProperty[值 : ""] is displayed in my table

转载 作者:行者123 更新时间:2023-12-02 23:57:01 25 4
gpt4 key购买 nike

我正在尝试在 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();

}

}

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

enter image description here

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

最佳答案

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

注意方法 valProperty。另请参阅this Q&A entry .

关于java - 为什么 StringProperty[值 : ""] is displayed in my table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31744795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com