2

我试图用数据填充 TableView。下一个代码在表中插入一个新行,但数据没有出现在表中。我试图为此找到解释但没有成功。

请帮忙。我不知道有什么问题。

在控制器.java

@FXML private TableView<TempTableData> tempTable;
@FXML private TableColumn<TempTableData,String> columnTime;
@FXML private TableColumn<TempTableData,Float> columnTempOne;
@FXML private TableColumn<TempTableData,Float> columnTempTwo;
@FXML private TableColumn<TempTableData,Float> columnTempThree;

@FXML protected void initialize() {


columnTime = new TableColumn<TempTableData,String>();
columnTime.setCellValueFactory(
    new PropertyValueFactory<TempTableData,String>("Time"));

columnTempOne = new TableColumn<TempTableData,Float>();
columnTempOne.setCellValueFactory(
    new PropertyValueFactory<TempTableData,Float>("Temp 1"));

columnTempTwo = new TableColumn<TempTableData,Float>();
columnTempTwo.setCellValueFactory(
    new PropertyValueFactory<TempTableData,Float>("Temp 2"));

columnTempThree = new TableColumn<TempTableData,Float>();
columnTempThree.setCellValueFactory(
    new PropertyValueFactory<TempTableData,Float>("Temp 3"));

tempDataList = FXCollections.observableArrayList();
tempDataList.add(new TempTableData("0",3.0f, 4f, 5f));
tempTable.setItems(tempDataList);
}

临时表数据.java

公共类 TempTableData {

private final SimpleStringProperty time;
private final SimpleFloatProperty dataSensorOne;
private final SimpleFloatProperty dataSensorTwo;
private final SimpleFloatProperty dataSensorThree;

public TempTableData(String time, float dataSensorOne, float dataSensorTwo, float dataSensorThree){
    this.time = new SimpleStringProperty(time);
    this.dataSensorOne = new SimpleFloatProperty(dataSensorOne);
    this.dataSensorTwo = new SimpleFloatProperty(dataSensorTwo);
    this.dataSensorThree = new SimpleFloatProperty(dataSensorThree);
}
public String getTime() {
    return time.get();
}

public void setTime(String time) {
    this.time.set(time);
}

public float getDataSensorOne() {
    return dataSensorOne.get();
}

public void setDataSensorOne(float dataSensorOne) {
    this.dataSensorOne.set(dataSensorOne);
}

public float getDataSensorTwo() {
    return dataSensorTwo.get();
}

public void setDataSensorTwo(float dataSensorTwo) {
    this.dataSensorTwo.set(dataSensorTwo);
}

public float getDataSensorThree() {
    return dataSensorThree.get();
}

public void setDataSensorThree(float dataSensorThree) {
    this.dataSensorThree.set(dataSensorThree);
}

public String toString(){
    String string = String.format("[time: %s | dataSensorOne: %f |dataSensorTwo: %f |dataSensorThree: %f ]", 
            time.get(), dataSensorOne.get(), dataSensorTwo.get(), dataSensorThree.get());
    return string;
}
}
4

2 回答 2

4

为什么要再次创建表格列?,因为他们已经使用 @FXML 注释(注入!)创建了。

从您的代码中删除列实例创建行,一切都会好起来的

// remove these lines
columnTime = new TableColumn<TempTableData,String>();
columnTempTwo = new TableColumn<TempTableData,Float>();
columnTempThree = new TableColumn<TempTableData,Float>();
于 2013-01-10T20:20:36.607 回答
1

确保您的单元工厂值与相应的模型类值完全拼写。例如 private final .... SimpleStringProperty time ; 和新的……PropertyValueFactory(" time "));

于 2014-03-01T07:29:29.390 回答