我正在进行实时数据库存收集,一切顺利,实时更新更新。我不是JavaFX的专家,我通过了文档,但仍然不太满意。
无论如何长话短说我要做的是:根据条件(不值得一提或代码发布)更改某个单元格的样式(行和列的交点)。
我有:
Stock
,其属性为variation
ObservableList<Stock> myStocksObservableList
包含我愿意展示的数据myStockTable
声明为TableView<Stock> myStockTable;
_variation
作为我感兴趣的列代码(前端):
声明:
@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列与我指定的行的交集,然后相应地更改其样式 ?
任何解决方案都会受到高度赞赏,也有任何建议!
答案 0 :(得分:2)
如果您可以提供包含要设置样式的项目的ObservableValue<Stock>
(或以允许您添加侦听器的方式存储信息的任何其他方式),则可以使用自定义TableRow
s将伪类添加到行并使用外部样式表设置单元格的样式:
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 */
}
答案 1 :(得分:0)
我认为没有办法直接获取表格单元格并对其进行修改。
但是,您可以尝试创建一个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]);
}