Javafx TableView没有显示数据 [英] Javafx TableView not showing data
问题描述
我使用 ObservableList
来填充 TableView
,但问题是数据没有显示在表I中不知道是什么问题因为行数与我添加的完全相同 capture 但细胞中没有任何东西!
这里是控制器的代码:
I used ObservableList
to populate the TableView
but the problem is that the data is not showing in the table I don't know what is the problem because the number of rows is exactly like I added them capture but there is nothing in the cells!
here is the code of the controller:
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);
}
}
和代码of 参数
class:
and the code of Parameter
class:
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);
}
}
推荐答案
你需要实际告诉 TableView
如何显示数据。这是使用 CellValueFactory
完成的。基本上,您需要告诉表中的每一列它保存的数据类型以及从哪里获取数据。
You need to actually tell the TableView
HOW to display the data. This is done using a CellValueFactory
. Basically, you need to tell each column of the table what type of data it holds and where it gets that data from.
您需要从定义列开始(给出它们是FXML文件或SceneBuilder中的 fx:id
:
You need to start by defining your columns (give them an fx:id
either in the FXML file or in SceneBuilder):
@FXML
TableColumn<Parameter, String> colCategory;
@FXML
TableColumn<Parameter, String> colSubCategory;
@FXML
TableColumn<Parameter, String> colSubSubCategory;
每个 TableColumn
需要两个Type参数。第一个定义了显示的对象(参数
)。第二个是此列的数据类型(所有这些都是 String
)。
Each TableColumn
takes two Type parameters. The first defines the object being displayed (Parameter
). The second is the data type for this column (all yours are String
).
定义列后,你需要在你的 initialize()
方法中设置 CellValueFactory
:
Once the columns are defined, you need to set their CellValueFactory
in your initialize()
method:
colCategory.setCellValueFactory(new PropertyValueFactory<Parameter, String>("cat"));
colSubCategory.setCellValueFactory(new PropertyValueFactory<Parameter, String>("subCat"));
colSubSubCategory.setCellValueFactory(new PropertyValueFactory<Parameter, String>("subSubCat"));
在这里,您要告诉每列要在哪里找到要显示的数据。引号中该行的最后一个参数是参数
对象中属性的名称。
Here you are telling each column where to find the data to be displayed. The last argument on the line, in the quotes, is the name of your property within the Parameter
object.
因此,当JavaFX填充您的表时,它将采用这些步骤来填充每一列(例如, colCategory
):
So, when JavaFX populates your table, it will takes these steps to populate each column (colCategory
, for example):
- 为
colCategory
获取CellValueFactory
。 - 工厂是
PropertyValueFactory
,因此确定哪个类包含该属性(在这种情况下,它是参数
类) - 通过名称在
参数
类中查找String
属性cat - 使用
cat
属性的值填充列的单元格。
- Get the
CellValueFactory
forcolCategory
. - The factory is a
PropertyValueFactory
, so determine which class holds the property (in this case it is theParameter
class) - Look in the
Parameter
class for aString
property by the name of "cat" - Populate the column's cell with the value of the
cat
property.
这篇关于Javafx TableView没有显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!