如何在 JavaFX 中通过 TableView 的行和列索引更改指定单元格的文本/颜色?

问题描述 投票:0回答:0

我正在寻找一种方法来更改由其行和列索引指定的表格单元格的颜色和文本 我试过这样的东西但它不起作用......

public void setCellColorAndText(int rowIndex, int columnIndex, Color color, String text, TableView<String> tableView) {
        // Get the cell at the specified row and column
        TableCell<String, String> cell = (TableCell<String, String>) tableView.getVisibleLeafColumn(columnIndex).getCellFactory().call(null);
        cell.setText(text);
        cell.setTextFill(color);

        // Set the style for the cell to change the background color
        String colorString = String.format("#%02X%02X%02X", (int) (color.getRed() * 255), (int) (color.getGreen() * 255), (int) (color.getBlue() * 255));
        cell.setStyle("-fx-background-color: " + colorString + ";");

        // Get the row for the specified index
        ObservableList<Node> rows = tableView.getChildrenUnmodifiable();
        TableRow<String> row = (TableRow<String>) rows.get(rowIndex);

        // Set the cell factory for the specified column to use the custom cell
        TableColumn<String, String> column = (TableColumn<String, String>) tableView.getVisibleLeafColumn(columnIndex);
        column.setCellFactory(tc -> cell);

        // Refresh the row to update the cell
        row.requestLayout();
    }
java javafx tableview
© www.soinside.com 2019 - 2024. All rights reserved.