【问题标题】:TableView for different models不同型号的 TableView
【发布时间】:2017-10-19 16:56:52
【问题描述】:

我正在尝试在 TableView 中实现以下目标:

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加入(学校项目)。我不认为这会成为一个问题,但现在它是。

我还尝试将三个模型作为列表包装到一个模型中,但我不确定如何保存模型之间的关系,以及如何在TableView 中显示它们。 (same as shown here)

如果有任何不清楚的地方请告诉我,以便我补充更多信息。

编辑;

澄清一下:

某些Product 可能不会在ProductNutrient 中包含每个Nutrient

例如:

prod_id | nutrient_id | amount |
1         1             0.0
1         2             5.1
2         1             1.4
3         1             0.2
3         2             0.0

这里prod_id 2 没有nutrient_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
}

【问题讨论】:

  • 模型类之间的关系对我来说不是很清楚。 Product 是否引用了 ProductNutrient?还是Nutrient?你能把这些类的代码贴出来吗?
  • @James_D ProductNutrient 表示连接表,它包含多个 p_id 和多个 nutrition_id,以及该营养素的数量。
  • 每个实例都为每个其他类保存多个 id?我真的不明白。我希望每个 ProductNutrient 实例引用一个 Product 实例和一个 Nutrient 实例。再说一遍,你不能在你的问题中发布这些类(或者至少是它们的简化版本来证明问题)吗?
  • @James_D 他们目前没有任何彼此的实例,我真的不知道如何实现。
  • 您实际上希望TableView 中的每一行代表连接表中的某些内容,对吗?列显示来自相应产品和相应营养素的数据?

标签: java javafx tableview


【解决方案1】:

我将类定义为

public class Product {

    private int id ;

    private String name ;
    private String comment ;

    // constructor and get methods

}
public class Nutrient {

    private int id ;

    private String name ;
    private double kcal ;

    // ...
}
public class ProductNutrientContent {

    private Product product ;
    private Nutrient nutrient ;
    private double quantity ;

    // ...
}

不能使用内连接的要求是相当人为的,但是只要数据库不是太大,你可以使用三个查询来获取ProductNutrient对象的列表,如下所示:

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());

【讨论】:

  • 有一个问题.. 营养素没有千卡的“价值”,所以在上面的例子中我不能cellData.getValue().getNutrient().getKcal() 吗?我没有得到Quantity 列,因为它的值应该在kcal 列下。
  • @MeesKluivers 不确定我是否关注。只需修改它,使其从实际存储的任何位置获取kcal
  • 该值存储在连接表本身中(在上述解决方案中的 ProductNutrientContent 中为 quantity ),所以我应该只为 kcal 列执行 cellData.getValue().getQuantity() 吗?
  • 通过上述解决方案,我仍然没有得到我需要的结果。 cellData.getValue().getQuantity() 只获得它找到的第一个,但在这种情况下,它与 Nutrient 的“千卡”或产品无关。我只是得到它在这种情况下发现的第一批数量。我会更新我的问题,希望能把事情弄清楚。
  • 在上面的示例中,Nutrient 中的 String name 是“列”(在实际的 sql 表中),kcal(和其他)被存储,所以double kcal 没有在我的情况下存在。
猜你喜欢
  • 2013-02-03
  • 2016-10-01
  • 2018-01-02
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多