java - 为什么 JavaFX TableView 不显示数据?

标签 java javafx tableview openjfx

我一直在做一个尝试学习 Java 的项目,但在让 Tableview 工作时遇到了麻烦。我已经查看了其他帖子,到目前为止我已经确定:

  • Getter 和 Setter 已正确设置和拼写
  • CellValueFactory 已为所有内容正确设置
  • CellValueFactory 中的变量名称与我的对象中的变量名称相同

但是,尽管如此,我仍然无法填充我的数据。编译此列表会创建一个带有占位符文本的空 TableView,尽管我的 println 显示列表和 tableview 都包含有效数据。

在此代码的实际版本中,用户可以添加到表格 View ,添加内容“显示”在列表中作为空白但可选择的条目。

GUI 类是

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.ArrayList;

public class GUI extends Application {

    //Variable initialization
    @FXML
    TableView tvTest=new TableView ();
    @FXML
    TableColumn<TestObj, Integer> keyCol=new TableColumn<>("Key");
    @FXML
    TableColumn<TestObj, String> aCol=new TableColumn<>("A");
    @FXML
    TableColumn<TestObj, String> bCol=new TableColumn<>("B");
    @FXML
    TableColumn<TestObj, String> cCol=new TableColumn<>("C");

    ArrayList<TestObj> testObjs=new ArrayList<TestObj>();
    ObservableList<TestObj> testObjList;

    //Sets up window
    @Override
    public void start(Stage primaryStage)throws IOException {
        primaryStage.setTitle("Table View Test");
        Parent root = FXMLLoader.load(getClass().getResource("tvTest.fxml"));
        Scene scene = new Scene(root,800,500);
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        setupTable();
        primaryStage.show();
    }

    //Creates cell factories and loads up lists
    private void setupTable(){

        //Test objects
        testObjs.add(new TestObj(1,"A1","B1","C1"));
        testObjs.add(new TestObj(2,"A2","B2","C2"));
        testObjs.add(new TestObj(3,"A3","B3","C3"));
        testObjList= FXCollections.observableList(testObjs);

        //Cell value factory creation
        keyCol.setCellValueFactory(new PropertyValueFactory<TestObj,Integer>("key"));
        aCol.setCellValueFactory(new PropertyValueFactory<TestObj,String>("A"));
        bCol.setCellValueFactory(new PropertyValueFactory<TestObj,String>("B"));
        cCol.setCellValueFactory(new PropertyValueFactory<TestObj,String>("C"));

        //Sets table data
        tvTest.setItems(testObjList);

        //Test to confirm
        System.out.println(testObjList.size());//Confirms 3 items in list
        System.out.println(tvTest.getItems().size());//Confirms 3 items in table

        //Make sure data is correct in list
        System.out.println("_____________________");
        for(int i = 0; i< testObjList.size(); i++){
            System.out.println(testObjList.get(i).getKey()+"|"+ testObjList.get(i).getA()+"|"+testObjList.get(i).getB()+"|"+ testObjList.get(i).getC());
        }
        System.out.println("_____________________");
    }
}

testObj 类是

public class TestObj {
    int key;
    String a;
    String b;
    String c;

    public int getKey() {
        return key;
    }

    public void setKey(int key) {
        this.key = key;
    }

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

    TestObj(int key, String A, String B, String C){
        this.key=key;
        this.a=A;
        this.b=B;
        this.c=C;
    }
}

FXML 文件是

<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="GUI">
   <children>
      <TableView layoutX="14.0" layoutY="14.0" prefHeight="372.0" prefWidth="570.0" fx:id="tvTest">
        <columns>
          <TableColumn fx:id="keyCol" prefWidth="72.0" text="Key" />
          <TableColumn fx:id="aCol" prefWidth="166.0" text="A" />
            <TableColumn fx:id="bCol" prefWidth="166.0" text="B" />
            <TableColumn fx:id="cCol" prefWidth="166.0" text="C" />
        </columns>
      </TableView>
   </children>
</AnchorPane>

如果有帮助的话,主类是

import javafx.application.Application;

public class Main {
    public static void main(String[] args) {
        Application.launch(GUI.class, args);
    }
}

我正在使用 maven 导入 o​​penJFX,除了这个 TableView 之外,它一直在工作。

再次,编译此列表会创建一个带有占位符文本的空 TableView,尽管我的 println 显示列表和 tableview 都包含有效数据。

如果有帮助:在此代码的真实版本中,用户可以添加到表格 View ,添加内容“显示”在列表中作为空白但可选择的条目。

非常感谢

最佳答案

您不需要初始化 @FXML 注解属性,因为它们将由 JavaFX 注入(inject)。

尝试这样:



    @FXML
    TableView tvTest;
    @FXML
    TableColumn keyCol;
    @FXML
    TableColumn aCol;
    @FXML
    TableColumn bCol;
    @FXML
    TableColumn cCol;

关于java - 为什么 JavaFX TableView 不显示数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57470540/

相关文章:

java - Hibernate 标准按出生之日起的年龄范围搜索

java - 死锁检测的成本如何?

java - 如何在 M1 MacBook 上使用 JavaFX?

java - 用数据库中的内容填充 TableView

ios - 滚动单元格双端队列时如何维护和保存自定义单元格文本字段输入数据

ios - 如何在 Realm 数组的特定索引处插入对象?

java - Junit:测试默认构造函数,死代码警告?

java - 测试/资源中文件的空指针异常

JavaFX 8 - Tabpanes 和选项卡,每个选项卡都有单独的 FXML 和 Controller

ios11中tableview trailingSwipeActionsConfigurationForRowAt破解