0

Let's consider we have the following informations :

enter image description here

As you see an article can be stored in many stores, and vice versa : a store can store many articles : that's the class model (UML )

some code : FXML Part :

 @FXML
     private  TableView<Article> tblArticles;
    @FXML
    private TableColumn<Article, String> colStore;

    @FXML
    private TableColumn<Article, Integer> colQuantity;

getters and setter :

colStore.setCellValueFactory(new PropertyValueFactory<>("store"));
colStore.setCellValueFactory(new PropertyValueFactory<>("quantity"));

I recieve the result seen in the first table but I am not able to do what is in the second table . enter image description here

And what I want exactly should give the following informations :

enter image description here

So my question is it possible to do this in a TableView ?

khalid tounoussi
  • 499
  • 5
  • 21
  • 1
    Yes, it is possible. You need to define custom table cells to do it. There are [tutorials for that](https://code.makery.ch/blog/javafx-8-tableview-cell-renderer/). Obviously for your implementation it will be more complex than the basic tutorial. I don't know if anybody will just post a solution for you if you don't provide some basic code [mcve] for what you have already tried to do. I get the feeling that to succeed in this you would need an actual concrete solution example rather than a set of pointers on how to do it. – jewelsea Sep 20 '19 at 18:36
  • Yes , I have seen the tutorial , I think it's not what I am looking for. I have made an edit . – khalid tounoussi Sep 20 '19 at 19:23

1 Answers1

3

Here is a sample app. It follows an MVVM style, which is appropriate for this kind of work. The app was built using Java 13 and will not work in earlier Java versions such as Java 8. It's a relatively long answer, but, ah well, sometimes that is what it takes.

The overall approach is not to create a tableview row for each store that an article is stored in. Instead, we just create a row for each article and we have a custom cell renderer which produces a single formatted cell for all the stores and quantities that that item is stored at.

Now, you could do an alternative implementation based upon a custom rowFactory. However, I do not recommend that approach for this particular task, as I believe it would be unnecessarily complicated to implement and maintain, without providing sufficient value.

Another way to do this is to use nested columns. This approach, when appropriate care is taken, does allow you to create a tableview row for each store that an article is stored in. If you do this, you need some way of populating different data depending on whether a row is either the first row in the group or not. You don't allow the user to reorder and sort data in the table, as that would be quite difficult to cater for because the notion of what is the "first row in the group" would be forever changing. To allow for appropriate rendering with nested columns, you end up with a slightly different view model (the FlatLineItem class below and the accompanying method in the LineItemService that retrieves them).

The image below demonstrates the output of a TableView with a custom cell renderer on the left and a TableView using nested columns on the right. Note how the selection works differently in each case. On the left when a row is selected, it includes all the stores that attached to that row. On the right when the nested columns are used, the row selection is only selecting a row for a given store.

sample app

Main application class

This sets up a couple of TableViews.

For the first table view, all it does is create a TableView with a column for each of the elements to be displayed. All the data is extracted from a LineItem view model class using a standard PropertyValueFactory. The slightly different thing is a custom cell renderer for a StoredQuantity field via the StoredQuantityTableCell, this is explained later.

The second view uses nested columns and works based upon the FlatLineItem view model class, also using a standard PropertyValueFactory and uses no custom cell renderer.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.util.List;

public class AggregateViewApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        LineItemService lineItemService = new LineItemService();

        TableView<LineItem> tableView = createArticleTableView();
        tableView.getItems().setAll(lineItemService.fetchAllLineItems());

        TableView<FlatLineItem> nestedTableView = createNestedArticleTableView();
        nestedTableView.getItems().setAll(lineItemService.fetchAllFlatLineItems());

        HBox layout = new HBox(
                40,
                tableView,
                nestedTableView
        );

        stage.setScene(new Scene(layout));
        stage.show();
    }

    @SuppressWarnings("unchecked")
    private TableView<LineItem> createArticleTableView() {
        TableView tableView = new TableView();

        TableColumn<LineItem, Long> articleIdCol = new TableColumn<>("Article ID");
        articleIdCol.setCellValueFactory(new PropertyValueFactory<>("articleId"));

        TableColumn<LineItem, String> nameCol = new TableColumn<>("Name");
        nameCol.setCellValueFactory(new PropertyValueFactory<>("articleName"));

        TableColumn<LineItem, List<StoredQuantity>> storedArticleCol = new TableColumn<>("Store Quantities");
        storedArticleCol.setCellValueFactory(new PropertyValueFactory<>("storedQuantities"));
        storedArticleCol.setCellFactory(lineItemStringTableColumn -> new StoredQuantityTableCell());

        TableColumn<LineItem, DB.StoredArticle> totalCol = new TableColumn<>("Total");
        totalCol.setCellValueFactory(new PropertyValueFactory<>("total"));

        tableView.getColumns().addAll(articleIdCol, nameCol, storedArticleCol, totalCol);

        tableView.setPrefSize(400, 150);

        return tableView;
    }

    @SuppressWarnings("unchecked")
    private TableView<FlatLineItem> createNestedArticleTableView() {
        TableView tableView = new TableView();

        TableColumn<FlatLineItem, Long> articleIdCol = new TableColumn<>("Article ID");
        articleIdCol.setCellValueFactory(new PropertyValueFactory<>("articleId"));
        articleIdCol.setSortable(false);

        TableColumn<FlatLineItem, String> nameCol = new TableColumn<>("Name");
        nameCol.setCellValueFactory(new PropertyValueFactory<>("articleName"));
        nameCol.setSortable(false);

        TableColumn<FlatLineItem, String> storeCol = new TableColumn<>("Store");
        storeCol.setCellValueFactory(new PropertyValueFactory<>("storeName"));
        storeCol.setSortable(false);
        TableColumn<FlatLineItem, String> storeQuantityCol = new TableColumn<>("Quantity");
        storeQuantityCol.setCellValueFactory(new PropertyValueFactory<>("storeQuantity"));
        storeQuantityCol.setSortable(false);

        TableColumn<FlatLineItem, List<StoredQuantity>> storedArticleCol = new TableColumn<>("Store Quantities");
        storedArticleCol.getColumns().setAll(
                storeCol,
                storeQuantityCol
        );
        storedArticleCol.setSortable(false);

        TableColumn<LineItem, DB.StoredArticle> totalCol = new TableColumn<>("Total");
        totalCol.setCellValueFactory(new PropertyValueFactory<>("total"));
        totalCol.setSortable(false);

        tableView.getColumns().setAll(articleIdCol, nameCol, storedArticleCol, totalCol);

        tableView.setPrefSize(400, 200);

        return tableView;
    }

    public static void main(String[] args) {
        launch(AggregateViewApp.class);
    }

}

StoredQuantityTableCell.java

This takes a list of StoredQuantities which is a tuple of a store name and a quantity of things stored at that store and then renders that list into a single cell, formatting the display internally in a GridView. You could use whatever internal node layout or formatting you wish and add CSS styling to spice things up if necessary.

import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.layout.GridPane;

import java.util.List;

class StoredQuantityTableCell extends TableCell<LineItem, List<StoredQuantity>> {
    private GridPane storedQuantityPane;

    public StoredQuantityTableCell() {
        storedQuantityPane = new GridPane();
        storedQuantityPane.setHgap(10);
        storedQuantityPane.setVgap(5);
    }

    @Override
    protected void updateItem(List<StoredQuantity> storedQuantities, boolean empty) {
        super.updateItem(storedQuantities, empty);

        if (storedQuantities == null)  {
            setGraphic(null);
            return;
        }

        storedQuantityPane.getChildren().removeAll(storedQuantityPane.getChildren());
        int row = 0;
        for (StoredQuantity storedQuantity: storedQuantities) {
            storedQuantityPane.addRow(
                    row,
                    new Label(storedQuantity.getStoreName()),
                    new Label("" + storedQuantity.getQuantity())
            );

            row++;
        }

        setGraphic(storedQuantityPane);
    }
}

LineItem.java

A view model class representing a row in the table.

import java.util.Collections;
import java.util.List;

public class LineItem {
    private long articleId;
    private String articleName;
    private List<StoredQuantity> storedQuantities;

    public LineItem(long articleId, String articleName, List<StoredQuantity> storedQuantities) {
        this.articleId = articleId;
        this.articleName = articleName;
        this.storedQuantities = storedQuantities;
    }

    public long getArticleId() {
        return articleId;
    }

    public String getArticleName() {
        return articleName;
    }

    public List<StoredQuantity> getStoredQuantities() {
        return Collections.unmodifiableList(storedQuantities);
    }

    public int getTotal() {
        return storedQuantities.stream()
                .mapToInt(StoredQuantity::getQuantity)
                .sum();
    }
}

StoredQuantity.java

A view model class representing a store name and quantity of things in the store. This is used by the StoredQuantityTableCell to render the stored quantities for a line item.

public class StoredQuantity implements Comparable<StoredQuantity> {
    private String storeName;
    private int quantity;

    StoredQuantity(String storeName, int quantity) {
        this.storeName = storeName;
        this.quantity = quantity;
    }

    public String getStoreName() {
        return storeName;
    }

    public int getQuantity() {
        return quantity;
    }

    @Override
    public int compareTo(StoredQuantity o) {
        return storeName.compareTo(o.storeName);
    }
}

FlatLineItem.java

A view model class supporting a table view with nested columns. A flat line item which can be created for each store that an article is stored in.

public class FlatLineItem {
    private Long articleId;
    private String articleName;
    private final String storeName;
    private final Integer storeQuantity;
    private final Integer total;
    private final boolean firstInGroup;

    public FlatLineItem(Long articleId, String articleName, String storeName, Integer storeQuantity, Integer total, boolean firstInGroup) {
        this.articleId = articleId;
        this.articleName = articleName;
        this.storeName = storeName;
        this.storeQuantity = storeQuantity;
        this.total = total;
        this.firstInGroup = firstInGroup;
    }

    public Long getArticleId() {
        return articleId;
    }

    public String getArticleName() {
        return articleName;
    }

    public String getStoreName() {
        return storeName;
    }

    public Integer getStoreQuantity() {
        return storeQuantity;
    }

    public Integer getTotal() {
        return total;
    }

    public boolean isFirstInGroup() {
        return firstInGroup;
    }
}

LineItemService.java

This translates values from the database into view model objects (LineItems or FlatLineItems) which can be rendered by the views. Note how the getFlatLineItemsForLineItem which constructs the FlatLineItems for the nested column table view has a notion of what it the first row in a group of line items and propagates the the FlatLineItem appropriately based on that, leaving some values null if they are just repeated from the first item in the group, which results in a clean display.

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class LineItemService {
    private final DB db = DB.instance();

    public List<LineItem> fetchAllLineItems() {
        return db.findAllArticles()
                .stream()
                .map(article -> createLineItemForArticle(article.getArticleId()))
                .collect(Collectors.toList());
    }

    public List<FlatLineItem> fetchAllFlatLineItems() {
        return fetchAllLineItems().stream()
                .flatMap(lineItem -> getFlatLineItemsForLineItem(lineItem).stream())
                .collect(Collectors.toList());
    }

    private List<FlatLineItem> getFlatLineItemsForLineItem(LineItem lineItem) {
        ArrayList<FlatLineItem> flatLineItems = new ArrayList<>();

        boolean firstStore = true;
        for (StoredQuantity storedQuantity: lineItem.getStoredQuantities()) {
            FlatLineItem newFlatLineItem;

            if (firstStore) {
                newFlatLineItem = new FlatLineItem(
                        lineItem.getArticleId(),
                        lineItem.getArticleName(),
                        storedQuantity.getStoreName(),
                        storedQuantity.getQuantity(),
                        lineItem.getTotal(),
                        true
                );

                firstStore = false;
            } else {
                newFlatLineItem = new FlatLineItem(
                        null,
                        null,
                        storedQuantity.getStoreName(),
                        storedQuantity.getQuantity(),
                        null,
                        false
                );
            }

            flatLineItems.add(newFlatLineItem);
        }

        return flatLineItems;
    }

    private LineItem createLineItemForArticle(long articleId) {
        DB.Article article =
                db.findArticleById(
                        articleId
                ).orElse(
                        new DB.Article(articleId, "N/A")
                );

        List<DB.StoredArticle> storedArticles =
                db.findAllStoredArticlesForArticleId(articleId);

        return new LineItem(
                article.getArticleId(),
                article.getName(),
                getStoredQuantitesForStoredArticles(storedArticles)
        );
    }

    private List<StoredQuantity> getStoredQuantitesForStoredArticles(List<DB.StoredArticle> storedArticles) {
        return storedArticles.stream()
                .map(storedArticle ->
                        new StoredQuantity(
                                db.findStoreById(storedArticle.getStoreId())
                                        .map(DB.Store::getName)
                                        .orElse("No Store"),
                                storedArticle.getQuantity()
                        )
                )
                .sorted()
                .collect(
                        Collectors.toList()
                );
    }
}

Mock database class

Just a simple in-memory representation of the database class. In a real app, you would probably use something like SpringData with hibernate to provide the data access repositories using a JPA based object to relational mapping.

The database classes aren't related to the view at all but are just presented here so that a running app can be created within a MVVM style framework.

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

class DB {
    private static final DB instance = new DB();
    public static DB instance() {
        return instance;
    }

    private List<Article> articles = List.of(
            new Article(1, "Hp101"),
            new Article(3, "Lenovo303"),
            new Article(4, "Asus404")
    );

    private List<Store> stores = List.of(
            new Store(1, "S1"),
            new Store(2, "S2")
    );

    private List<StoredArticle> storedArticles = List.of(
            new StoredArticle(1, 1, 30),
            new StoredArticle(1, 2, 70),
            new StoredArticle(3, 1, 50),
            new StoredArticle(4, 2, 70)
    );

    public Optional<Article> findArticleById(long articleId) {
        return articles.stream()
                .filter(article -> article.getArticleId() == articleId)
                .findFirst();
    }

    public Optional<Store> findStoreById(long storeId) {
        return stores.stream()
                .filter(store -> store.getStoreId() == storeId)
                .findFirst();
    }

    public List<StoredArticle> findAllStoredArticlesForArticleId(long articleId) {
        return storedArticles.stream()
                .filter(storedArticle -> storedArticle.articleId == articleId)
                .collect(Collectors.toList());
    }

    public List<Article> findAllArticles() {
        return Collections.unmodifiableList(articles);
    }

    static class Article {
        private long articleId;
        private String name;

        public Article(long articleId, String name) {
            this.articleId = articleId;
            this.name = name;
        }

        public long getArticleId() {
            return articleId;
        }

        public String getName() {
            return name;
        }
    }

    static class Store {
        private long storeId;
        private String name;

        public Store(long storeId, String name) {
            this.storeId = storeId;
            this.name = name;
        }

        public long getStoreId() {
            return storeId;
        }

        public String getName() {
            return name;
        }
    }

    static class StoredArticle {
        private long articleId;
        private long storeId;
        private int quantity;

        public StoredArticle(long articleId, long storeId, int quantity) {
            this.articleId = articleId;
            this.storeId = storeId;
            this.quantity = quantity;
        }

        public long getArticleId() {
            return articleId;
        }

        public long getStoreId() {
            return storeId;
        }

        public int getQuantity() {
            return quantity;
        }
    }
}

Answers to some follow-up questions

Which Approach is the best for updating data ?

All of the approaches I have shown use read only data models and views. To make it read-write would be a bit more work (and out of scope for what I would be prepared to add to this already long answer). Probably, of the two approaches outlined above, the approach which uses a separate row for each store containing an item would be the easiest to adapt to making the data updatable.

Which approach in general I should use to update data ( data are stored for sure in db) ?

Defining a general approach to updating data in a database is out of scope for what I would answer here (it is a purely opinion based answer, as there are many different ways to accomplish this, and as such is off topic for StackOverflow). If it were me, I'd set up a SpringBoot based rest service that connected to the database and have my client app communicate with that. If the app does not need to communicate over the internet, but only communicate with a local DB over a LAN, then adding direct database access by making the app a SpringBoot app and using Spring Data repositories with the embedded H2 database is what I would use.

Is when modifying in a specific row modify in db or wait until user modify in the whole tableview and click on a save button ?

Either way would work, I don't have any strong opinion on one versus the other. I'd probably lean towards the immediate update scenario rather than a delayed save scenario, but it would depend on the app and desired user experience.

Please can you provide me with some code for either to draw a line under every cell or to make it just like usual tableView ( one row gray and one not etc ...)

You can ask that as a separate question. But, in general, use CSS styling. If you use the second approach outlined above which has a row per store, then everything is already a "usual tableView" in terms of styling, with one row gray and one row not, etc., so I don't know that any additional styling is really required in such a case.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • I think you have given me all what I need. thanks, I want to ask you please another questions, 1- wich Approach is the best for updating data ? 2- and wich approach in general I should use to update data ( data are stored for sure in db) ,3- is when modifying in a specific row modify in db or wait until user modify in the whole tableview and click on a save button ? – khalid tounoussi Sep 21 '19 at 11:09
  • Please can you provide me with some code for either to draw a line under every cell or to make it just like usual tableView ( one row gray and one not etc ...) – khalid tounoussi Sep 21 '19 at 12:58
  • Updated answer to address follow-up questions. – jewelsea Sep 23 '19 at 17:44