gpt4 book ai didi

tableview - javaFX Tableview 数据不可见

转载 作者:行者123 更新时间:2023-12-02 05:18:24 27 4
gpt4 key购买 nike

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

请帮忙。我不知道怎么了。

在 controller.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);
}

TempTableData.java

公共(public)类 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;
}
}

最佳答案

为什么要重新创建表格列? ,因为它们已经使用 @FXML 注释创建(注入(inject)!)。

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

// remove these lines
columnTime = new TableColumn<TempTableData,String>();
columnTempTwo = new TableColumn<TempTableData,Float>();
columnTempThree = new TableColumn<TempTableData,Float>();

关于tableview - javaFX Tableview 数据不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14266461/

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