javaFX Tableview数据不可见

时间:2013-01-10 20:12:28

标签: tableview javafx

我尝试用数据填充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 class 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;
}
}

2 个答案:

答案 0 :(得分:4)

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

从代码中删除列实例创建行,一切都会正常

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

答案 1 :(得分:1)

确保您的单元格工厂值拼写与相应的模型类值完全相同。 例如 私人决赛.... SimpleStringProperty 时间; 和 新...... PropertyValueFactory(“时间”));