Java 不同型号的TableView
我试图在TableView中实现以下目标:Java 不同型号的TableView,java,javafx,tableview,Java,Javafx,Tableview,我试图在TableView中实现以下目标: prod_name | prod_comment | ... | kcal | kjoule | -------------------------------------------- name | hi | | 1.5 | 5 name | hi | | 2.0 | 7 以下是我使用的模型: Product (holds things like name,c
prod_name | prod_comment | ... | kcal | kjoule |
--------------------------------------------
name | hi | | 1.5 | 5
name | hi | | 2.0 | 7
以下是我使用的模型:
Product (holds things like name,comment etc)
Nutrient (holds the kcal, kjoule strings etc)
Product_Nutrient (holds the amounts of kcal, kjoule etc for every product)
我目前拥有的是一个DAO(和一个单独的模型)层,它连接所有这些表并以上面的格式返回它们,但是对于这个项目,不允许使用SQL连接(学校项目)。我不认为这会成为一个问题,但现在它是
我还尝试将三个模型作为列表包装到一个模型中,但我不确定如何保持模型之间的关系,以及如何在表视图中显示它们。()
如果有任何不清楚的地方,请告诉我,以便我可以添加更多信息
编辑
需要澄清的是:
一些产品
可能没有产品营养素
中的所有营养素
例如:
这里的prod\u id
2没有prod\u id
2,因为0.0
不同于不存在
编辑:
型号:
public class ProductModel {
private String name;
private int prod_code;
....
//getters/setters
}
public class ProductNutrientModel {
private double amount; // amount of the nutrient linked to a product
private int prod_code;
private int nutrient_id;
....
//getters/setters
}
public class NutrientModel {
private int nutrient_id;
private String name; // names of the nutrients
....
// Nutrient holds no amount
//getters/setters
}
我将这些类定义为
public class Product {
private int id ;
private String name ;
private String comment ;
// constructor and get methods
}
不能使用内部联接的要求是相当人为的,但只要数据库不太大,就可以使用三个查询来获取productfunt
对象的列表,如下所示:
Map<Integer, Product> productsById ;
Map<Integer, Nutrient> nutrientsById ;
List<ProductNutientContent> productNutrientContents ;
// query product table, for each row in the result set do:
Product product = new Product(/* data from result set row */);
productsById.put(product.getId(), product);
// similarly populate nutrientsById...
// query join table, for each row in join table do:
int productId = ... ; // productId from row in result set
int nutrientId = ... ; // nutrientId from row in result set
double quantity = ... ; // quantity from row in result set
ProductNutrientContent content
= new ProductNutrientContent(productsById.get(productId), nutrientsById.get(nutrientId), quantity);
productNutrientContents.add(content);
地图产品byid;
地图营养素b;
列出产品目录;
//查询产品表,对于结果集中的每一行执行以下操作:
产品产品=新产品(/*来自结果集行的数据*/);
productsById.put(product.getId(),product);
//同样,填充nutrientsById。。。
//查询联接表,对于联接表中的每一行,请执行以下操作:
int productId=…;//结果集中行的productId
int nutrientId=…;//结果集中行的nutrientId
双倍数量=…;//结果集中来自行的数量
产品营养素含量
=新产品NutrientContent(productsById.get(productId)、nutrientsById.get(nutrientId)、数量);
productNutrientContents.add(内容);
现在,您可以使用列表填充表。对于表列,只需执行显而易见的操作:
TableColumn<ProductNutrientContent, String> productNameColumn = new TableColumn<>("Product");
productNameColumn.setCellValueFactory(cellData ->
new SimpleStringProperty(cellData.getValue().getProduct().getName()));
TableColumn<ProductNutrientContent, Number> kcalColumn = new TableColumn<>("kcal");
kcalColumn.setCellValueFactory(cellData ->
new SimpleDoubleProperty(cellData.getValue().getNutrient().getKcal()));
TableColumn<ProductNutrientContent, Number> quantityColumn = new TableColumn<>("Quantity");
quantityColumn.setCellValueFactory(cellData ->
new SimpleDoubleProperty(cellData.getValue().getQuantity());
TableColumn productNameColumn=新的TableColumn(“产品”);
productNameColumn.setCellValueFactory(cellData->
新的SimpleStringProperty(cellData.getValue().getProduct().getName());
TableColumn kcalColumn=新的TableColumn(“kcal”);
kcalColumn.setCellValueFactory(cellData->
新的SimpleDoubleProperty(cellData.getValue().getNutrient().getKcal());
TableColumn quantityColumn=新的TableColumn(“数量”);
quantityColumn.setCellValueFactory(cellData->
新的SimpleDoubleProperty(cellData.getValue().getQuantity());
我不太清楚模型类之间的关系。Product
是否引用了productfuniture
?或者productfuniture
?您可以发布这些类的代码吗?@James\u D productfuniture表示联接表,它包含多个p\u id和多个p\u id,以及amo营养素的unt。每个实例都有其他每个类的多个ID?我真的不明白。我希望每个ProductNutrient
实例引用一个Product
实例和一个nutrient
实例。再说一次,你不能在你的问题中发布这些类吗(或者至少是证明问题的简化版本)?@James\u D他们目前没有任何实例,我真的不知道如何实现这一点。实际上,您希望TableView
中的每一行表示联接表中的某个内容,对吗?列显示来自相应产品和相应营养素的数据?有一个问题。。营养素没有kcal的“值”,因此在上面的示例中,我无法执行cellData.getValue().getNutrient().getKcal()
?我没有得到数量
列,因为它的值应该在kcal
列下。@MeesKluivers不确定我是否遵循。只需修改它,使它从实际存储的位置得到kcal
。该值存储在联接表本身中(在上述解决方案的ProductNutrientContent
中,作为quantity
),我应该只对kcal
列执行cellData.getValue().getQuantity()
?使用上述解决方案,我仍然无法获得所需的结果。cellData.getValue().getQuantity()
只获取它找到的第一个,但它与本例中营养素的“kcal”或产品没有关联。我只是获取它在本例中找到的第一个量。我将更新我的问题,希望能澄清问题。在上面的示例中,营养素中的字符串名称
是“列”(在实际的sql表中)存储了kcal
(以及其他),因此在我的案例中不存在双kcal
。
public class ProductNutrientContent {
private Product product ;
private Nutrient nutrient ;
private double quantity ;
// ...
}
Map<Integer, Product> productsById ;
Map<Integer, Nutrient> nutrientsById ;
List<ProductNutientContent> productNutrientContents ;
// query product table, for each row in the result set do:
Product product = new Product(/* data from result set row */);
productsById.put(product.getId(), product);
// similarly populate nutrientsById...
// query join table, for each row in join table do:
int productId = ... ; // productId from row in result set
int nutrientId = ... ; // nutrientId from row in result set
double quantity = ... ; // quantity from row in result set
ProductNutrientContent content
= new ProductNutrientContent(productsById.get(productId), nutrientsById.get(nutrientId), quantity);
productNutrientContents.add(content);
TableColumn<ProductNutrientContent, String> productNameColumn = new TableColumn<>("Product");
productNameColumn.setCellValueFactory(cellData ->
new SimpleStringProperty(cellData.getValue().getProduct().getName()));
TableColumn<ProductNutrientContent, Number> kcalColumn = new TableColumn<>("kcal");
kcalColumn.setCellValueFactory(cellData ->
new SimpleDoubleProperty(cellData.getValue().getNutrient().getKcal()));
TableColumn<ProductNutrientContent, Number> quantityColumn = new TableColumn<>("Quantity");
quantityColumn.setCellValueFactory(cellData ->
new SimpleDoubleProperty(cellData.getValue().getQuantity());