简体   繁体   English

获取JavaFX TableCell以更改其样式

[英]Get JavaFX TableCell in order to change its style

I'm working on real time data stock gathering, everything works smoothly with real time update on change. 我正在进行实时数据库存收集,所有工作都可以随着更改的实时更新而顺利进行。 I'm not an expert in JavaFX I went through documentation but still not very comfortable with it. 我不是JavaFX的专家,我阅读过文档,但仍然不太满意。

Anyway long story short what I'm trying to do is: Change the style of certain Cell ( Intersection of a Row and Column ) depending on a condition (not worth mentioning or code posting). 总之,长话短说,我想要做的是:根据条件(不值得一提或发布代码)更改某些Cell的样式( 行和列的交集 )。


I have: 我有:

  • class Stock with an attribute variation 类具有属性variation Stock
  • An observable list ObservableList<Stock> myStocksObservableList contains the data I'm willing to display 可观察的列表ObservableList<Stock> myStocksObservableList包含我愿意显示的数据
  • TableView myStockTable declared as such TableView<Stock> myStockTable; TableView myStockTable声明为这样的TableView<Stock> myStockTable;
  • A set of TableColumns with _variation as the column in which I'm interested 一组带有_variation作为我感兴趣的列的TableColumns

Code (front end): 代码(前端):

Declaration: 宣言:

@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;

Initialize (let's say myStocksObservableList is already filled with a list of Stock 's: 初始化(假设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);

How can I add a listener or any such trick that can check a specific TableCell value that will be the intersection of the column _variation and a row I specify, then changes its style accordingly ? 我如何添加一个监听器或任何这样的把戏,可以查看特定的TableCell值,这将是该列_variation和行我指定的路口,然后相应地改变它的风格?

Any solution would be strongly appreciated, any suggestion as well! 任何解决方案将不胜感激,任何建议!

If you can provide a ObservableValue<Stock> that contains the item to style (or any other way of storing the info in a way that allows you to add a listener), you can use custom TableRow s to add a pseudoclass to the row and style the cell using an external stylesheet: 如果您可以提供一个ObservableValue<Stock> ,其中包含要设置样式的项目(或任何其他允许以添加侦听器的方式存储信息的方式),则可以使用自定义TableRow将伪类添加到行中,并使用外部样式表为单元格设置样式:

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 stylesheet CSS样式表

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

I don't think there is a way to directly get a table cell and modify it. 我认为没有办法直接获取表格单元格并对其进行修改。

However, you can try to create a CellFactory that changes the cell's properties (eg style) according to the updates of the cell. 但是,您可以尝试创建一个CellFactory ,以根据单元格的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]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM