Java FX-用不同的颜色填充表格行

时间:2018-11-11 18:41:53

标签: java javafx

我更新了代码,以显示整个类的样子 还是有一些错误,您可以验证要使其正常运行需要改进的地方

主要问题在于fillTableView()方法

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 个答案:

答案 0 :(得分:0)

您需要通过覆盖updateItem()方法来更新表视图的项目。

代码是:

  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");
                }
            }
        };
    });
相关问题