I need to add color to row and column based on pnl value. How can I change the color of the row and how do I get pnl value for determining change in the color of the row.
TreeTableColumn<ClosedTradesPnL, String> symColumn = new TreeTableColumn<>("Symbol");
symColumn.setPrefWidth(100);
symColumn.setCellValueFactory(
(TreeTableColumn.CellDataFeatures<ClosedTradesPnL, String> param) ->
new ReadOnlyStringWrapper(param.getValue().getValue().getSymbol())
);
TreeTableColumn<ClosedTradesPnL, Date> expiryColumn =
new TreeTableColumn<>("Expiry Date");
expiryColumn.setPrefWidth(100);
expiryColumn.setCellValueFactory(
(TreeTableColumn.CellDataFeatures<ClosedTradesPnL, Date> param) ->
new ReadOnlyObjectWrapper(param.getValue().getValue().getExpiry_date())
);
TreeTableColumn<ClosedTradesPnL, String> pnlColumn =
new TreeTableColumn<>("PnL");
pnlColumn.setPrefWidth(100);
// pnlColumn.setStyle(" -fx-background-color: red ;");
// pnlColumn.setCellValueFactory(
// (TreeTableColumn.CellDataFeatures<ClosedTradesPnL, String> param) ->
// new ReadOnlyStringWrapper(param.getValue().getValue().getRealized_PNL())
// );
pnlColumn.setCellValueFactory(new Callback<CellDataFeatures<ClosedTradesPnL, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(CellDataFeatures<ClosedTradesPnL, String> p) {
int foo = Integer.parseInt(p.getValue().getValue().getRealized_PNL().replace(",", "").replace(".", ""));
if( foo == 0){
System.out.println("color app"+p.getValue().getValue().getRealized_PNL());
pnlColumn.setStyle(" -fx-background-color: red ;");
}else{
pnlColumn.setStyle("-fx-background-color: white ;");
}
System.out.println(p.getValue().getValue().getRealized_PNL());
return new ReadOnlyObjectWrapper(p.getValue().getValue().getRealized_PNL());
}
});
TreeTableView<ClosedTradesPnL> treeTableView = new TreeTableView<>(root);
treeTableView.getColumns().setAll(symColumn, expiryColumn,pnlColumn);
// pnlColumn.setStyle("-fx-alignment: center-right;-fx-control-inner-background: slateblue;");
sceneRoot.getChildren().add(treeTableView);
// if (treeTableView.getRow(root)){treeTableView.setBackground(Background.RED);}
stage.setScene(scene);
stage.show();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…