0

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

zero_cool
  • 3
  • 2
  • Any help in this regard is highly appreciated. – zero_cool Jan 04 '18 at 10:47
  • Did you tried, to use a second level in the `TreeTableView`, that should work exactly the same that you would like. – Sunflame Jan 04 '18 at 11:08
  • 3
    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 Jan 04 '18 at 13:42
  • @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 Jan 04 '18 at 17:16
  • @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 Jan 04 '18 at 17:17

1 Answers1

0

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
Sunflame
  • 2,993
  • 4
  • 24
  • 48
  • I got it to working by making it dynamically second level children, however i can partially accept the Sunflame answer. so accepting it. That is instead of making employee as my main root parent now i am making department my second level parent and hiding the employee root element. – zero_cool Jan 09 '18 at 07:50