我有一段代码可以为TableView
创建数据。它不应该有错误或一些问题。但是TableView
没有显示数据。在调试模式下,TableView
具有正确的数据。我无法解决这个问题。谢谢你的帮助。
控制器:
public class ServersController {
@FXML
TableView<SQLServerProp> table;
@FXML
TableColumn<SQLServerProp, String> colSNum;
@FXML
TableColumn<SQLServerProp, String> colSpec;
@FXML
TableColumn<SQLServerProp, Integer> colCpu;
@FXML
TableColumn<SQLServerProp, Double> colMem;
public PostgreSQLJDBC PSQL;
public void setPSQL(PostgreSQLJDBC PSQL) {
this.PSQL = PSQL;
}
public ServersController()
{
table = new TableView<SQLServerProp>();
colCpu = new TableColumn<SQLServerProp, Integer>("cpu");
colSpec = new TableColumn<SQLServerProp, String>("specific");
colMem = new TableColumn<SQLServerProp, Double>("memory");
colSNum = new TableColumn<SQLServerProp, String>("snumber");
}
public void LoadData()
{
List<SQLServerProp> dataServers = PSQL.GetDataServers();
colSNum.setCellValueFactory(new PropertyValueFactory<SQLServerProp, String>("snumber"));
colSpec.setCellValueFactory(new PropertyValueFactory<SQLServerProp, String>("specific"));
colCpu.setCellValueFactory(new PropertyValueFactory<SQLServerProp, Integer>("cpu"));
colMem.setCellValueFactory(new PropertyValueFactory<SQLServerProp, Double>("memory"));
ObservableList<SQLServerProp> dataServersO = FXCollections.observableArrayList(dataServers);
table.setItems(dataServersO);
table.getColumns().addAll(colSNum, colSpec, colCpu, colMem);
}
模型(仅针对snumber的getter和setter(例如),其他字段有类比getter和setter):
public class SQLServerProp {
public final SimpleStringProperty snumber;
public final SimpleStringProperty specific;
public final SimpleDoubleProperty memory;
public final SimpleIntegerProperty cpu;
public SQLServerProp(String sNumber, String specific, Double memory, Integer cpu) {
this.snumber = new SimpleStringProperty(sNumber);
this.specific = new SimpleStringProperty(specific);
this.memory = new SimpleDoubleProperty(memory);
this.cpu = new SimpleIntegerProperty(cpu);
}
public String getSnumber() {
return snumber.get();
}
public StringProperty snumberProperty() {
return snumber;
}
public void setSnumber(String snumber) {
this.snumber.set(snumber);
}
带窗口的XML TableView:
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="438.0" prefWidth="596.0" xmlns="http://javafx.com/javafx/8.0.131" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ptk.controller.ServersController">
<children>
<TableView layoutX="14.0" layoutY="14.0" prefHeight="418.0" prefWidth="466.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="116.0" AnchorPane.topAnchor="10.0">
<columns>
<TableColumn fx:id="colSNum" prefWidth="94.0" text="Serial Number" />
<TableColumn fx:id="colSpec" prefWidth="237.0" text="Specification" />
<TableColumn fx:id="colCpu" prefWidth="59.0" text="CPU, kernel" />
<TableColumn fx:id="colMem" prefWidth="79.0" text="Memory, Gb" />
</columns>
</TableView>
</children>
</AnchorPane>