Javafx:TableView根据列值更改行颜色

时间:2015-06-17 11:05:00

标签: javafx javafx-2 javafx-8

我有以下代码来更新列单元格及其相应行的颜色:

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

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

                TableRow currentRow = getTableRow();

                //This doesn't work
                if(item.equals("a")){
                    item.setTextFill(Color.RED);
                    currentRow.setTextFill(Color.PINK);
                    }
                else{
                    item.setTextFill(Color.GREEN);
                    currentRow.setTextFill(Color.BLUE);
                }

            }
        };
    });

&#39;如果&#39;的代码段条件不起作用。我无法识别对象的正确引用,以及执行此操作的最佳方法。

谢谢!

4 个答案:

答案 0 :(得分:12)

private void customiseFactory(TableColumn<CallLogs, String> calltypel) {
    calltypel.setCellFactory(column -> {
        return new TableCell<CallLogs, String>() {
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);

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

                TableRow<CallLogs> currentRow = getTableRow();

                if (!isEmpty()) {

                    if(item.equals("a")) 
                        currentRow.setStyle("-fx-background-color:lightcoral");
                    else
                        currentRow.setStyle("-fx-background-color:lightgreen");
                }
            }
        };
    });
}

这有效!

答案 1 :(得分:4)

我最近对这个问题进行了一些研究。使用以下代码,您可以根据列值更改TableView的行颜色(我将尽力解释它)。

我们要做的第一件事是定义TableView和TableView的列:

private TableView<Person> personTable;
private TableColumn<Person, String> nameColumn;
private TableColumn<Person, String> lastNameColumn;

下一步是定义其中一列的单元格工厂:

nameColumn.setCellFactory(column -> {
    return new TableCell<Person, String>() {
        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty); //This is mandatory

            if (item == null || empty) { //If the cell is empty
                setText(null);
                setStyle("");
            } else { //If the cell is not empty

                setText(item); //Put the String data in the cell

                //We get here all the info of the Person of this row
                Person auxPerson = getTableView().getItems().get(getIndex());

                // Style all persons wich name is "Edgard"
                if (auxPerson.getName().equals("Edgard")) {
                    setTextFill(Color.RED); //The text in red
                    setStyle("-fx-background-color: yellow"); //The background of the cell in yellow
                } else {
                    //Here I see if the row of this cell is selected or not
                    if(getTableView().getSelectionModel().getSelectedItems().contains(auxPerson))
                        setTextFill(Color.WHITE);
                    else
                        setTextFill(Color.BLACK);
                }
            }
        }
    };
});

代码的逻辑:我们覆盖的updateItem()方法,它在基础项发生变化时自动调用。

我们收到必须呈现的数据项(在本例中为String)。如果该项为空或null(例如空单元格),则不应用任何样式。否则,我们格式化项目,设置单元格的文本,以及颜色和背景,具体取决于人员的名称。

如果要在表格的其他列中应用此单元格的颜色,我们必须使用“行工厂”而不是“单元工厂”,但代码的逻辑类似:

personTable.setRowFactory(row -> new TableRow<Person>(){
    @Override
    public void updateItem(Person item, boolean empty){
        super.updateItem(item, empty);

        if (item == null || empty) {
            setStyle("");
        } else {
            //Now 'item' has all the info of the Person in this row
            if (item.getName().equals("Edgar")) {
                //We apply now the changes in all the cells of the row
                for(int i=0; i<getChildren().size();i++){
                    ((Labeled) getChildren().get(i)).setTextFill(Color.RED);
                    ((Labeled) getChildren().get(i)).setStyle("-fx-background-color: yellow");
                }                        
            } else {
                if(getTableView().getSelectionModel().getSelectedItems().contains(item)){
                    for(int i=0; i<getChildren().size();i++){
                        ((Labeled) getChildren().get(i)).setTextFill(Color.WHITE);;
                    }
                }
                else{
                    for(int i=0; i<getChildren().size();i++){
                        ((Labeled) getChildren().get(i)).setTextFill(Color.BLACK);;
                    }
                }
            }
        }
    }
});

这是我发现在行的所有单元格中应用样式更改的最佳方法。如果在Cell Factory中使用方法“getTableRow()”,则无法修改其单元格子项。

注意1:如果要更改文本的样式,则必须在单元格中工作。如果您尝试直接在行上进行此更改,则无效。

注意2:如果您使用的是单独的CSS文件,请不要写这样的内容:

.table-cell {
    -fx-text-fill: Black;
}

因为如果这样做,所有Java代码都没有效果。

答案 2 :(得分:0)

正确的方法是使用表格的table.setRowFactory(tv -> new TableRow<CustomItem>() { @Override protected void updateItem(CustomItemitem, boolean empty) { super.updateItem(item, empty); if (item == null || item.getValue() == null) setStyle(""); else if (item.getValue() > 0) setStyle("-fx-background-color: #baffba;"); else if (item.getValue() < 0) setStyle("-fx-background-color: #ffd7d1;"); else setStyle(""); } });

push di

答案 3 :(得分:0)

经过多次搜索,我找到了答案。您需要为表中的特定行设置ID,并根据行ID在外部CSS文件中设置颜色。这是我将错误行的颜色更改为红色的示例。 Java代码:

string filename;
filename = Path.GetFileName(fullFilename);

Workbook.Windows[filename].WindowState = Excel.XlWindowState.xlMinimized;
Workbook.Windows[filename].WindowState = Excel.XlWindowState.xlNormal;

// you can also use Worksheet.Activate() here if you want

CSS文件:

resultsTable.setRowFactory(row -> new TableRow<Result>() {
            @Override
            public void updateItem(Result item, boolean empty) {
                super.updateItem(item, empty);
                if (item == null) {
                    setStyle("");
                } else if (item.getResultType().equalsIgnoreCase("Error")) {
                    this.setId("error");
                } else {
                    this.setId("not-error");
                }
            }
        });