我无法弄清楚为什么我的TableView没有设置数据。看似一切都很好,但是这些行只是不会出现。我查看了教程,但我仍然没有看到我的错误。这是我的代码。 数据类:
package model.elastic;
import javafx.beans.property.SimpleStringProperty;
public class ElasticAttribute {
private SimpleStringProperty name;
private SimpleStringProperty type;
private SimpleStringProperty variable;
public ElasticAttribute(String name, String type, String variable) {
this.name = new SimpleStringProperty(name);
this.type = new SimpleStringProperty(type);
this.variable = new SimpleStringProperty(variable);
}
public String getName() {
return name.get();
}
public void setName(String name) {
this.name.set(name);
}
public String getType() {
return type.get();
}
public void setType(String type) {
this.type.set(type);
}
public String getVariable() {
return variable.get();
}
public void setVariable(String variable) {
this.variable.set(variable);
}
}
控制器:
@FXML
private TableView<ElasticAttribute> tableView;
private TableColumn tableColumnName;
private TableColumn tableColumnType;
private TableColumn tableColumnVariable;
public void initialize() {
final ObservableList<ElasticAttribute> data = FXCollections.observableArrayList(
new ElasticAttribute("Test", "Test", "Test"),
new ElasticAttribute("Test2", "Test", "Test"),
new ElasticAttribute("Test3", "Test", "Test"),
new ElasticAttribute("Test4", "Test", "Test"),
new ElasticAttribute("Test5", "Test", "Test")
);
tableColumnName = new TableColumn("Name");
tableColumnName.setCellValueFactory(
new PropertyValueFactory<ElasticAttribute, String>("name"));
tableColumnType = new TableColumn("Type");
tableColumnType.setCellValueFactory(
new PropertyValueFactory<ElasticAttribute, String>("type"));
tableColumnVariable = new TableColumn("Variable");
tableColumnVariable.setCellValueFactory(
new PropertyValueFactory<ElasticAttribute, String>("variable"));
tableView.setItems(data);
tableView.getColumns().addAll(tableColumnName, tableColumnType, tableColumnVariable);
fxml:
<TableView fx:id="tableView" GridPane.rowIndex="1">
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
</TableView>
感谢您的帮助。