如何在TableView中为具有特定条件的特定行着色?

时间:2019-08-22 10:20:19

标签: javafx javafx-8 javafx-2

我正在使用jfxml创建我的表视图并使用Controller对其进行操作。 在控制器中,我想对单元格进行特定操作,并用不同的颜色进行更改,这是TextFieldTableCell。

还有一些操作代码:

coldisc.setCellFactory(TextFieldTableCell.forTableColumn());
    coldisc.setOnEditCommit(e -> {
        e.getTableView().getItems().get(e.getTablePosition().getRow()).setDisc(e.getNewValue());
        Brand b = tablebrand.getSelectionModel().getSelectedItem();
        TablePosition pos2 = tablebrand.getFocusModel().getFocusedCell();
        Calc();
        CalcDisc();

        //**********
        Platform.runLater(() -> tablebrand.edit(pos2.getRow(), colafterdisc));
        //**********

        if (pos2.getRow() == tablebrand.getItems().size() - 1) {
            tablebrand.getItems().add(new Brand("", "", "", "", "", "", ++ts));
        }

        Platform.runLater(() -> tablebrand.edit(pos2.getRow() + 1, colcode));
        tablebrand.getSelectionModel().clearAndSelect(pos2.getRow() + 1, colcode);
        final TablePosition pos3 = tablebrand.getFocusModel().getFocusedCell();
        CalcTot();
          }
    });

2 个答案:

答案 0 :(得分:0)

一种方法是在需要的地方有条件地向行添加样式类:

final String styleClass = "color-class";
tableView.setRowFactory(new Callback<TableView<Foo>, TableRow<Foo>>() {
    @Override
    public TableRow<Foo> call(TableView<Foo> fooTableView) {
        return new TableRow<Foo>() {
            @Override
            protected void updateItem(Foo foo, boolean empty) {
                super.updateItem(foo, empty);
                if (/* condition */) {
                    getStyleClass().add(styleClass);
                } else { /* remove if condition no longer true */
                    getStyleClass().remove(styleClass);
                }
            }
        };
    }
});

然后在css文件中,添加所需的颜色:

.table-row-cell.color-class {
    -fx-background-color: /*your color*/;
}

答案 1 :(得分:0)

这可以使用setStyle方法。

如果满足特定条件,则添加:setStyle(“您的自定义样式”);

如果没有,只需添加setStyle(“”);

让我们弄清楚它是如何工作的,假设您想突出显示具有空值的行:

    // styling javafx tablerow with null values -> background color : red

    Callback<TableView<T>, TableRow<T>> rowFactory = new Callback<TableView<T>, TableRow<T>>() {

        @Override
        public TableRow<T> call(TableView<T> l) {
            return new TableRow<T>() {

                @Override
                protected void updateItem(T item, boolean empty) {
                    super.updateItem(item, empty);

                    try {
                        // isNull() is a custom method to catch null values in a row
                        if (isNull()) {

                            setStyle("-fx-background-color: #ff8080;");

                        } else {

                            // No empty cells - No specific highlights

                            setStyle("");

                        }

                    } catch (NullPointerException e) {

                    }

                }
            };
        }
    };

    tv.setRowFactory(rowFactory);
相关问题