TableView JavaFx APP中的空白行 [英] blank Row in TableView JavaFx APP
问题描述
我想在我的javafx应用程序中使用tableview,但它在tableview中给我空行.....
这是示例输出:
i am trying to use tableview in my javafx app but it's giving me blank rows in tableview..... Here is sample output :
以下是模型类的代码:
public class Pizza {
private final SimpleStringProperty pname;
private final SimpleStringProperty pflavour;
private final SimpleStringProperty psize;
private final SimpleDoubleProperty pprice;
public Pizza(String name, String flavour, String size, Double price) {
super();
this.pname = new SimpleStringProperty(name);
this.pflavour = new SimpleStringProperty( flavour);
this.psize = new SimpleStringProperty(size);
this.pprice = new SimpleDoubleProperty(price);
}
public String getName() {
return pname.get();
}
public String getFlavour() {
return pflavour.get();
}
public String getSize() {
return psize.get();
}
public Double getPrice() {
return pprice.get();
}
}
以下是控制器类的代码:
Here is the code for controller class :
@FXML
private TableView<Pizza> pizzatable;
@FXML
private TableColumn<Pizza,String> pname;
@FXML
private TableColumn<Pizza,String> pflavour;
@FXML
private TableColumn<Pizza,String> psize;
@FXML
private TableColumn<Pizza,Double> pprice;
ObservableList<Pizza> pizzalist;
这是我调用从数据库中获取数据的方法的代码:
Here is the code for method that i call to fetch data from databse:
public void fetchpizza() throws SQLException{
stmt = conn.createStatement();
sql = "select * from items where itype = 'Pizza'";
rs = stmt.executeQuery(sql);
pname.setCellValueFactory(new PropertyValueFactory<Pizza,String>("pname"));
pflavour.setCellValueFactory(new PropertyValueFactory<Pizza,String>("pflavour"));
psize.setCellValueFactory(new PropertyValueFactory<Pizza,String>("psize"));
pprice.setCellValueFactory(new PropertyValueFactory<Pizza,Double>("pprice"));
pizzalist = FXCollections.observableArrayList();
while(rs.next()){
pizzalist.add(new Pizza(rs.getString("oname"),rs.getString("flavour"),rs.getString("size"),rs.getDouble("price")));
pizzatable.setItems(pizzalist);
}
rs.close();
stmt.close();
}
推荐答案
你需要使用 String
s对应于getter或属性getter作为 PropertyValueFactory
构造函数的参数。
You need to use the String
s corresponding to the getters or property getters as argument for the PropertyValueFactory
constructor.
例如如果将name
作为参数传递给 PropertyValueFactory<的构造函数,则将使用以下方法之一检索值/属性; Pizza,String>
:
E.g. one of the following methods of the items would be used to retrieve the value/property, if you pass "name"
as parameter to the constructor of PropertyValueFactory<Pizza, String>
:
-
nameProperty()
(应该返回StringProperty
)。这允许观察值的变化而无需调用TableView.refresh()
。 -
getName( )
(应该返回字符串
)
nameProperty()
(should returnStringProperty
). This allows to observe changes in the value without having to callTableView.refresh()
.getName()
(should returnString
)
你但是传递字段的名称。由于方法名称不会进行所需的模式,因此 TableCell
仍为空。
You pass the name of the fields however. Since the method names do not march the required pattern, the TableCell
s remain empty.
这篇关于TableView JavaFx APP中的空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!