Java FX - filling table rows in different colours

半世苍凉 提交于 2019-12-31 05:26:25

问题


I updated my code to show how the whole class look like Still I got some errors can you validate what I need to improve to have it working

Mainly the problem is with fillTableView() method

private TableCell fillTableView() {

    clientColumn.setCellFactory(column -> {
        return new TableCell<CarFx, String>() {
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);

                setText(empty ? "" : getItem().toString());
                setGraphic(null);

                TableRow<CarFx> currentRow = getTableRow();

                if (!isEmpty()) {

                    if (item.equals("EMPTY EMPTY"))
                        currentRow.setStyle("-fx-background-color:green");
                    else
                        currentRow.setStyle("-fx-background-color:blue");
                }
            }
        };
    });

}

回答1:


You need to update the items of Table View by overriding updateItem() method.

The code is :

  yourColumnBased.setCellFactory(column -> {
        return new TableCell<CarFx, String>() {
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);

                setText(empty ? "" : getItem().toString());
                setGraphic(null);

                TableRow<CarFx> currentRow = getTableRow();

                if (!isEmpty()) {

                    if(item.equals("assigned")) 
                        currentRow.setStyle("-fx-background-color:blue");
                    else
                        currentRow.setStyle("-fx-background-color:green");
                }
            }
        };
    });


来源:https://stackoverflow.com/questions/53251956/java-fx-filling-table-rows-in-different-colours

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!