我正在尝试在我的javafx应用程序中使用tableview,但它在tableview中给我空白行..... 以下是示例输出:
以下是模型类的代码:
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();
}
}
以下是控制器类的代码:
@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;
以下是我调用从数据库中获取数据的方法的代码:
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();
}
答案 0 :(得分:0)
您需要使用与getter或property getters对应的String
作为PropertyValueFactory
构造函数的参数。
E.g。如果将"name"
作为参数传递给PropertyValueFactory<Pizza, String>
的构造函数,则会使用以下某个项目方法之一来检索值/属性:
nameProperty()
(应该返回StringProperty
)。这样可以观察值的变化,而无需调用TableView.refresh()
。getName()
(应该返回String
)但是,您传递了字段的名称。由于方法名称不会进行所需的模式,因此TableCell
仍为空。