Javafx TableView没有显示数据

时间:2018-06-03 20:37:49

标签: javafx tableview

我使用ObservableList来填充TableView,但问题是数据没有显示在表格中我不知道是什么问题因为行数和我添加的完全一样他们capture但细胞中什么都没有! 这是控制器的代码:

public class EnlistDim {

    private static final String DEFAULT="-fx-text-background-color: black; -fx-background-color: steelblue;-fx-fill: red ;";
    @FXML
    private TableView<Parameter> tab;


    @FXML
    public void initialize() {
        final ObservableList<Parameter> data = FXCollections.observableArrayList(
        new Parameter("Query","Access method","Sequential scan"),
                new Parameter("Query","Access method","in memory"),
        new Parameter("Query","Operation","join"),
        new Parameter("Query","Operation","Scan"),
        new Parameter("Query","Operation","Sort"),
        new Parameter("Database","Buffer management","Without buffer"),
        new Parameter("Database","Buffer management","FIFO"),
        new Parameter("Database","Buffer management","LIFO"),
        new Parameter("Database","Buffer management","LRU"),
        new Parameter("Database","Buffer management","Other"),

        new Parameter("Database","Optimization structure","Not used"),
        new Parameter("Database","Optimization structure","Partionning"),
        new Parameter("Database","Optimization structure","Materialized View"),
        new Parameter("Database","Optimization structure","compresssion"),

        new Parameter("Database","System storage type","Database SQL"),
        new Parameter("Database","System storage type","New SQL"),
        new Parameter("Database","System storage type","Document"),
        new Parameter("Database","System storage type","Graph"),
        new Parameter("Database","System storage type","NVRAM"),
        new Parameter("Database","System storage type","key value store"),

        new Parameter("Database","Data storage type","Row Oriented"),
        new Parameter("Database","Data storage type","Column Oriented"),
        new Parameter("Database","Data storage type","Hybrid Oriented"),

        new Parameter("Hardware","Processing device","CPU"),
        new Parameter("Hardware","Processing device","GPU"),
        new Parameter("Hardware","Processing device","FPGA"),


        new Parameter("Hardware","Storage device","RAM"),
        new Parameter("Hardware","Storage device","SSD"),
        new Parameter("Hardware","Storage device","NVRAM"),


        new Parameter("Hardware","Communication device","Modem"),
        new Parameter("Hardware","Communication device","Cable"),
        new Parameter("Hardware","Communication device","FaxModem"),
        new Parameter("Hardware","Communication device","Router")

           );
        tab.setEditable(true);
        tab.setItems(data);



        tab.setStyle(DEFAULT);

    }

}

Parameter类的代码:

class  Parameter {

   SimpleStringProperty cat;
   SimpleStringProperty subCat;
   SimpleStringProperty subSubCat;

        Parameter(String cat, String subCat, String subSubCat) {
        this.cat = new SimpleStringProperty(cat);
        this.subCat = new SimpleStringProperty(subCat);
        this.subSubCat = new SimpleStringProperty(subSubCat);
    }

    public String getCat() {
        return cat.get();
    }
    public void setCat(String c) {
        cat.set(c);
    }

    public String getSubCat() {
        return subCat.get();
    }
    public void setSubCat(String sc) {
        subCat.set(sc);
    }

    public String getSubSubCat() {
        return subSubCat.get();
    }
    public void setSubSubCat(String ssc) {
        subSubCat.set(ssc);
    }

}

1 个答案:

答案 0 :(得分:0)

您需要实际告诉TableView如何显示数据。这是使用CellValueFactory完成的。基本上,您需要告诉表中的每一列它包含的数据类型以及从中获取数据的位置。

您需要首先定义列(在FXML文件或SceneBuilder中为它们提供fx:id):

@FXML
TableColumn<Parameter, String> colCategory;
@FXML
TableColumn<Parameter, String> colSubCategory;
@FXML
TableColumn<Parameter, String> colSubSubCategory;

每个TableColumn都有两个Type参数。第一个定义要显示的对象(Parameter)。第二个是此列的数据类型(所有这些都是String)。

定义列后,您需要在CellValueFactory方法中设置initialize()

colCategory.setCellValueFactory(new PropertyValueFactory<Parameter, String>("cat"));
colSubCategory.setCellValueFactory(new PropertyValueFactory<Parameter, String>("subCat"));
colSubSubCategory.setCellValueFactory(new PropertyValueFactory<Parameter, String>("subSubCat"));

在这里,您告诉每列要在哪里找到要显示的数据。引号中该行的最后一个参数是Parameter对象中属性的名称。

因此,当JavaFX填充您的表时,它将采用这些步骤来填充每列(例如colCategory):

  1. 获取CellValueFactory的{​​{1}}。
  2. 工厂是colCategory,因此确定哪个类包含属性(在本例中为PropertyValueFactory类)
  3. Parameter课程中查找名为“cat”的Parameter媒体资源
  4. 使用String属性的值填充列的单元格。