我已经在这方面做了一段时间并且似乎无法解决它,我试图从我的数据库填充一个tableview,tableview填充了适当数量的行但是所有的细胞仍然空白。 值为Integer,String,Double,Int,String
这是加载tableview的类
public class FXMLDocumentTakeOrderController implements Initializable {
@FXML private TableView <Item> tableviewLightMeals;
@FXML private TableColumn<?, ?> colItemNumber;
@FXML private TableColumn<?, ?> colItemName;
@FXML private TableColumn<?, ?> colItemCategory;
@FXML private TableColumn<?, ?> colItemPrice;
@FXML private TableColumn<?, ?> colItemIngredients;
ObservableList <Item> data = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
colItemNumber.setCellValueFactory(new PropertyValueFactory<>("itemid"));
colItemName.setCellValueFactory(new PropertyValueFactory<>("itemname"));
colItemCategory.setCellValueFactory(new PropertyValueFactory<>("itemcategory"));
colItemPrice.setCellValueFactory(new PropertyValueFactory<>("itemprice"));
colItemIngredients.setCellValueFactory(new PropertyValueFactory<>("itemingredients"));
loadMenuItems();
}
public void loadMenuItems(){
Connection con;
try{
String username = "register01";
String password = "jxncjdshvbfdkhg";
String url = "jdbc:mysql://mydatabaselink";
con = DriverManager.getConnection(url, username, password);
Statement sqlStat = con.createStatement();
ResultSet rs = sqlStat.executeQuery("select * from menu where itemcategory='1'" + ";");
while(rs.next()){
data.add(new Item(
rs.getInt("itemid"),
rs.getString("itemname"),
rs.getInt("itemcategory"),
rs.getDouble("itemprice"),
rs.getString("itemingredients")
));
tableviewLightMeals.setItems(data);
//System.out.println(ToStringBuilder.reflectionToString(Item));
}
sqlStat.close();
rs.close();
}catch(Exception e){
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
}
这是我的物品类
public class Item {
private int id;
private String name;
private int category;
private double price;
private String ingredients;
public Item (int id, String name, int category, double price, String ingredients) {
this.id = id;
this.name = name;
this.category = category;
this.price = price;
this.ingredients = ingredients;
}
public void setItemId (int id){
this.id = id;
}
public void setItemName (String name){
this.name = name;
}
public void setItemCategory (int category){
this.category = category;
}
public void setItemPrice (double price){
this.price = price;
}
public void setItemIngredients (String ingredients){
this.ingredients = ingredients;
}
// Getters
public int getItemId (){
return id;
}
public String getItemName (){
return name;
}
public int getItemCategory (){
return category;
}
public double getItemPrice (){
return price;
}
public String getItemIngredients (){
return ingredients;
}
}
我认为我的吸气者是正确的,正如我在这里看到的另一个人的问题,这是一个非常相似的问题,但我仍然无法解决这个问题。