[英]Get JavaFX TableCell in order to change its style
我正在进行实时数据库存收集,所有工作都可以随着更改的实时更新而顺利进行。 我不是JavaFX的专家,我阅读过文档,但仍然不太满意。
总之,长话短说,我想要做的是:根据条件(不值得一提或发布代码)更改某些Cell的样式( 行和列的交集 )。
我有:
variation
Stock
ObservableList<Stock> myStocksObservableList
包含我愿意显示的数据 myStockTable
声明为这样的TableView<Stock> myStockTable;
_variation
作为我感兴趣的列的TableColumns 代码(前端):
宣言:
@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和行我指定的路口,然后相应地改变它的风格?
任何解决方案将不胜感激,任何建议!
如果您可以提供一个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();
}
});
.table-row-cell:marked .table-cell.variation {
-fx-font-style: italic; /* or some other style */
}
我认为没有办法直接获取表格单元格并对其进行修改。
但是,您可以尝试创建一个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.