0

I need to set a color of row in TreeTable based on column value. Although I have already found two answers for such a question: javafx adding color to row based on column value

Javafx: TableView change row color based on column value

,unfortunately I can not find a way how to implement it in my code? From code below TreeTable works OK. Let's say I want now to click button "check staff" and update TreeTable with 2 contitions: 1 - mark green color rows where Managers name is "John"

enter image description here

2 - mark yellow color rows where Days Absence are > 4

Can somebody explain how to do it in my code?

enter image description here

public void showStaffInTreeTable(){

        ArrayList<Employee> johnStaff = new ArrayList<>(Arrays.asList(new Employee("1", "secretary", "1"),
                new Employee("2", "cleaner", "2")));
        ArrayList<Employee> amandaStaff = new ArrayList<>(Arrays.asList(new Employee("3", "driver", "0"),
                new Employee("4", "mechanic", "5")));

        ArrayList<Manager> allStaff = new ArrayList<>(Arrays.asList(new Manager("John", johnStaff),
                new Manager("Amanda", amandaStaff)));

        TreeTableColumn<Staff_DTO, String> columnManager = new TreeTableColumn<>("Manager");
        TreeTableColumn<Staff_DTO, String> columnStaffId = new TreeTableColumn<>("Id");
        TreeTableColumn<Staff_DTO, String> columnStaffWorkplace = new TreeTableColumn<>("Workplace");
        TreeTableColumn<Staff_DTO, String> columnStaffAbsence = new TreeTableColumn<>("Days Absence");

        columnManager.setCellValueFactory(new TreeItemPropertyValueFactory<>("managersName"));
        columnStaffId.setCellValueFactory(new TreeItemPropertyValueFactory<>("id"));
        columnStaffWorkplace.setCellValueFactory(new TreeItemPropertyValueFactory<>("workplace"));
        columnStaffAbsence.setCellValueFactory(new TreeItemPropertyValueFactory<>("daysAbsence"));
        
        treeTableStaff.getColumns().addAll(columnManager, columnStaffId, columnStaffWorkplace, columnStaffAbsence);

        TreeItem root = new TreeItem(new Staff_DTO("", "","",""));
        for (Manager manager: allStaff){
            TreeItem managerItem = new TreeItem(new Staff_DTO(manager.getManagersName(), "","",""));
            for (Employee employee: manager.getManagersStaff()){
                managerItem.getChildren().add(new TreeItem<>(new Staff_DTO("",employee.getId(), employee.getWorkplace(), employee.getDaysAbsence())));
            }
            root.getChildren().add(managerItem);
        }
        
        treeTableStaff.setRoot(root);
        anchorPaneTreeTableStaff.getChildren().add(treeTableStaff);
    }
public void buttonCheckStaff(){

    }
pawchi
  • 77
  • 7
  • [This question](https://stackoverflow.com/questions/32119277/colouring-table-row-in-javafx) shows how to do this in a `TableView`, and essentially the same technique works in a `TreeTableView`. If you can't get it to work, provide a [mre] demonstrating the problem. – James_D Jun 23 '20 at 12:37
  • wow, this explanation is clear and works!! Thanks a lot James_D ! – pawchi Jun 23 '20 at 13:19

0 Answers0