0
votes

I have a treetableview that i am using to display list of employees with organisation as root item. Now i want to group tree items based on the department they work in. For example: currently i am displaying as mentioned below:

current

However i would like to have something as mentioned below i.e., the employees grouped by their department, keeping in the view that employee name and department are part of same object Employee.java:

needed

1
Any help in this regard is highly appreciated. - zero_cool
Did you tried, to use a second level in the TreeTableView, that should work exactly the same that you would like. - Sunflame
You did manage to group the employees by company. What prevents you from using the same approach to group the employees of each company by department? - fabian
@fabian i made company as root, now i want intermediate roots i.e., department to be root, so could not get a way to get through it. So company is a static root element in my case where as employees are dynamic. - zero_cool
@sunflame can you please provide more details on how to use the second level in the treetableview. It would be of great help. Thanks in advance! - zero_cool

1 Answers

0
votes

Here is the solution you should use:

Instead of Object use your model.

    TreeTableView<Object> treeTableView = new TreeTableView<>();

    TreeItem<Object> root = new TreeItem<>();
    // if you need more roots you can disable the root and use the first level children as roots otherwise ignore this line.
    treeTableView.setShowRoot(false);
    treeTableView.setRoot(root);

    // first level children
    TreeItem<Object> firstLevelChild1 = new TreeItem<>();
    TreeItem<Object> firstLevelChild2 = new TreeItem<>();

    root.getChildren().add(firstLevelChild1);
    root.getChildren().add(firstLevelChild2);

    // second level children

    TreeItem<Object> secondLevelChild1 = new TreeItem<>();
    TreeItem<Object> secondLevelChild2 = new TreeItem<>();

    firstLevelChild1.getChildren().add(secondLevelChild1);
    firstLevelChild2.getChildren().add(secondLevelChild2);

    // third level children

    TreeItem<Object> thirdLevelChild1 = new TreeItem<>();
    TreeItem<Object> thirdLevelChild2 = new TreeItem<>();

    secondLevelChild2.getChildren().add(thirdLevelChild1);
    secondLevelChild2.getChildren().add(thirdLevelChild2);

    // .... and so on you can define as many levels you want