Sli*_*lim 2 java javafx javafx-2 javafx-8
我试图在a中显示一些信息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;
}
}
Run Code Online (Sandbox Code Playgroud)
而我正试图在我的桌子上添加一些行.这是我的整个代码:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
但我的表看起来像这样:
我在这里错过了什么?我知道它很小,但作为一个新手,我无法发现它.你能帮我推吗?
您没有遵循Room数据模型中的JavaFX特定访问器语法:
如果该字段是ObservableValue,如:
private StringProperty val = new SimpleStringProperty();
Run Code Online (Sandbox Code Playgroud)
应该有访问者:
public String getVal() {
val.get();
}
public void setVal(String newVal) {
val.set(newVal);
}
public StringProperty valProperty() {
return val;
}
Run Code Online (Sandbox Code Playgroud)
注意方法val Property.另请参阅此问答条目.
| 归档时间: |
|
| 查看次数: |
1710 次 |
| 最近记录: |