Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX Coloring TableCell

I need your help!

I've got a Table with rows in it (Name, etc..) Now, I want to color a specific tableCells background when the Object, laying on this row has got a specific value. But i only get it to read the value of this cell. But i need to read the Object (in my code called TableListObject) to know in wich color i need to color the cell. But this "color value" is not visible (has no column) in that row.

Here is my code:

for(TableColumn tc:tView.getColumns()) {
    if(tc.getId().equals("text")) {
        tc.setCellValueFactory(newPropertyValueFactory<TableListObject,String>("text"));
        // here i need to check the Objects value and coloring that cell
    }
}

Here is a HTML Fiddle to visualize my problem: https://jsfiddle.net/02ho4p6e/

like image 557
Mr. 0x50 Avatar asked Jan 28 '16 08:01

Mr. 0x50


Video Answer


1 Answers

Call the cell factory for the column you want and override the updateItem method. You need to check if it is empty and then if it is not you can do your object check and then you can set the color of the cell background or any other style you want. Hope this helps.

    tc.setCellFactory(column -> {
        return new TableCell<TableListObject, String>() {
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);

                if (item == null || empty) {
                    setText(null);
                    setStyle("");
                } else {
                    if (item.equals("Something")) {
                        setStyle("-fx-background-color: blue");
                    } else {
                        setStyle("");
                    }
                }
            }
        };
    });

EDIT 1:

If you want to use the value of another cell in the same row. You will have to use the index of the row and get the items need for the check.

tc.setCellFactory(column - > {
   return new TableCell < TableListObject, String > () {
     protected void updateItem(String item, boolean empty) {
       super.updateItem(item, empty);

       if (item == null || empty) {
         setText(null);
         setStyle("");
       } else {
         int rowIndex = getTableRow().getIndex();
         String valueInSecondaryCell = getTableView().getItems().get(rowIndex).getMethod();
         if (valueInSecondaryCell.equals("Something Else")) {
           setStyle("-fx-background-color: yellow"); //Set the style in the first cell based on the value of the second cell
         } else {
           setStyle("");
         }

       }
     }
   };
 });

EDIT 2:

Improved answer based on suggestion. This uses the referenced object.

   else {
         TableListObject listObject = (TableListObject) getTableRow().getItem();
         if (listObject.getMethod().equals("Something Else")) {
           setStyle("-fx-background-color: yellow"); //Set the style in the first cell based on the value of the second cell
         } else {
           setStyle("");
         }
       }
like image 197
Iulian Gheorghita Avatar answered Oct 11 '22 19:10

Iulian Gheorghita