【问题标题】:javaFX Tableview Data not visiblejavaFX Tableview 数据不可见
【发布时间】:2012-12-25 08:15:59
【问题描述】:

我尽一切努力用数据填充 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

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

【问题讨论】:

  • 我在您的代码中看不到“插入新行”。参考oracle官方的tableview教程,里面可以找到向表中插入新行的代码。

标签: tableview javafx


【解决方案1】:

确保您的单元工厂值与对应的模型类值完全拼写。 例如 private final .... SimpleStringProperty 时间; 和 new ...... PropertyValueFactory("时间"));

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 不变量,谢谢!我正在再次创建列。这解决了我的问题。顺便说一句,如果我评论该列: TempThree.setCellValueFactory( new PropertyValueFactory("Temp 3"));
    • 不变量,谢谢!我正在再次创建列。这解决了我在字符串列中的问题,但我仍然遇到浮点值问题。我阅读了 TableView 并且在文档中行类型是一个泛型类,但所有示例都使用字符串。它只适用于字符串吗?
    • 我在属性方面还有一点需要改变:columnTempOne.setCellValueFactory(new PropertyValueFactory("Temp 1"));。我需要更改 dataSensorOne 的“Temp 1”。谢谢你的帮助!!
    猜你喜欢
    • 2015-10-01
    • 2017-12-25
    • 2020-02-07
    • 2014-06-03
    • 2018-11-13
    • 1970-01-01
    • 2016-02-16
    • 2012-12-08
    • 2023-01-26
    相关资源
    最近更新 更多