获取 JavaFX TableCell 以更改其样式

Get JavaFX TableCell in order to change its style

我正在收集实时数据库存,一切都在实时更新变化的情况下顺利进行。我不是 JavaFX 的专家,我浏览了文档,但仍然不太适应。

总之,长话短说,我想做的是:根据条件更改某些单元格的样式(行和列的交集)(不值得一提或代码发布)。


我有:


代码(前端):

声明:

@FXML
private TableView<Stock> mystockTable;
@FXML
private TableColumn<Stock, String> _symbol;
@FXML
private TableColumn<Stock, Float> _open;
@FXML
private TableColumn<Stock, Float> _high;
@FXML
private TableColumn<Stock, Float> _low;
@FXML
private TableColumn<Stock, Float> _close;
@FXML
private TableColumn<Stock, Long> _volume;
@FXML
private TableColumn<Stock, Float> _variation;
@FXML
private TableColumn<Stock, String> _comment;
@FXML
private ObservableList<Stock> myStocksObservableList;

初始化(假设 myStocksObservableList 已经充满了 Stock 的列表:

    _symbol.setCellValueFactory(new PropertyValueFactory<>("symbol"));
    _open.setCellValueFactory(new PropertyValueFactory<>("open"));
    _high.setCellValueFactory(new PropertyValueFactory<>("high"));
    _low.setCellValueFactory(new PropertyValueFactory<>("low"));
    _close.setCellValueFactory(new PropertyValueFactory<>("close"));
    _volume.setCellValueFactory(new PropertyValueFactory<>("volume"));
    _variation.setCellValueFactory(new PropertyValueFactory<>("variation"));
    _comment.setCellValueFactory(new PropertyValueFactory<>("comment"));
     mystockTable.setItems(myStocksObservableList);

我如何添加监听器或任何此类技巧来检查特定 TableCell 值,该值将成为列 _variation 和我指定的行的交集,然后相应地更改其样式?

非常感谢任何解决方案,以及任何建议!

我认为没有办法直接获取 table 单元格并对其进行修改。

不过,您可以尝试创建一个CellFactory根据单元格的更新来更改单元格的属性(例如样式)。

Callback factory = new Callback<TableColumn<String[], String>, TableCell<String[], String>>() {
    @Override
    public TableCell<String[], String> call(TableColumn<String[], String> param)
    {
        return new TableCell<String[], String>() {
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);

                // depending on item's value do something. . .
                // for instance, if item == 'bla' change cell color 
                // this.setStyle(.....)

                // don't forget to set item's text
                setText(item);
            }
        };
    }
};

// your table data in the from of String[][] where the first row holds the column titles
//String[][] data = ...
tableView.getColumns().clear();
tableView.getItems().clear();

// populating the tableView
for (int rowIndex = 0 ; rowIndex < data.length ; rowIndex++)
{
    for (int columnIndex = tableView.getColumns().size(); columnIndex < data[rowIndex].length; columnIndex++)
    {
        TableColumn<String[], String> column = new TableColumn<>();
        final int index = columnIndex;
        column.setCellFactory(factory); // Important line
        column.setCellValueFactory(cellData -> {
            String[] row = cellData.getValue();
            String value = row[index];
            return new SimpleStringProperty(value);
        });
        tableView.getColumns().add(column);
    }
    tableView.getItems().add(data[rowIndex]);
}

如果您可以提供包含要设置样式的项目的 ObservableValue<Stock>(或以允许您添加侦听器的方式存储信息的任何其他方式),则可以使用自定义 TableRows 将伪类添加到行并使用外部样式表设置单元格样式:

ObservableValue<Stock> markedItem = ...;
PseudoClass marked = PseudoClass.getPseudoClass("marked");
_variation.getStyleClass().add("variation");

mystockTable.setRowFactory(tv -> new TableRow<Stock>() {

    private void updatePseudoclass() {
        pseudoClassStateChanged(marked, !isEmpty() && getItem() == markedItem.getValue());
    }

    {
        markedItem.addListener(o -> updatePseudoclass());
    }

    @Override
    protected void updateItem(Stock item, boolean empty) {
        super.updateItem(item, empty);
        updatePseudoclass();
    }

});

CSS 样式表

.table-row-cell:marked .table-cell.variation {
    -fx-font-style: italic; /* or some other style */
}