0

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();
    }
brian
  • 10,619
  • 4
  • 21
  • 79
jaya sankar
  • 39
  • 2
  • 13
  • Do you want to color the whole row or just the pnl cell? You also say you want to color the column, but that doesn't make sense as it has many pnl values in one column. – brian Feb 05 '15 at 13:35
  • Next time you should include a small program that compiles. I had to type in the missing parts myself. – brian Feb 05 '15 at 19:49
  • This question is pretty similar to: [Updating TableView row appearance](http://stackoverflow.com/questions/16153838/updating-tableview-row-appearance) – jewelsea Feb 05 '15 at 20:52

1 Answers1

2

This should be self explanatory, I've included a few comments.

import java.util.Date;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyDoubleWrapper;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableCell;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableRow;
import javafx.scene.control.TreeTableView;
import javafx.stage.Stage;

public class StyleTableRow extends Application {

    @Override
    public void start(Stage primaryStage) {
        TreeTableView<ClosedTradesPnL> ttv = new TreeTableView<>(new TreeItem<>(new ClosedTradesPnL()));
        for (int i=0;i<10;i++) ttv.getRoot().getChildren().add(new TreeItem<>(new ClosedTradesPnL()));
        ttv.getRoot().setExpanded(true);

        TreeTableColumn<ClosedTradesPnL, String> symColumn = new TreeTableColumn<>("Symbol");
        symColumn.setCellValueFactory((param) -> 
            new ReadOnlyStringWrapper(param.getValue().getValue().getSymbol())
        );

        TreeTableColumn<ClosedTradesPnL, Date> expiryColumn = new TreeTableColumn<>("Expiry Date");
        expiryColumn.setCellValueFactory((param) -> 
            new ReadOnlyObjectWrapper<>(param.getValue().getValue().getExpiry_date())
        );

        //use Number for type of data in column
        TreeTableColumn<ClosedTradesPnL, Number> pnlColumn = new TreeTableColumn<>("PnL");
        pnlColumn.setCellValueFactory((param) -> 
            new ReadOnlyDoubleWrapper(param.getValue().getValue().getRealized_PNL())
        );

        //now use a cellFactory to style the cell
        //you can get the row and style it as well
        pnlColumn.setCellFactory((TreeTableColumn<ClosedTradesPnL, Number> param) -> {
            TreeTableCell cell = new TreeTableCell<ClosedTradesPnL, Number>(){
                @Override
                //by using Number we don't have to parse a String
                protected void updateItem(Number item, boolean empty) {
                    super.updateItem(item, empty);
                    TreeTableRow<ClosedTradesPnL> ttr = getTreeTableRow();
                    if (item == null || empty){
                        setText(null);
                        ttr.setStyle("");
                        setStyle("");
                    } else {
                        ttr.setStyle(item.doubleValue() > 0 
                                ? "-fx-background-color:lightgreen"
                                : "-fx-background-color:pink");
                        setText(item.toString());
                        setStyle(item.doubleValue() > 0 
                                ? "-fx-background-color:green"
                                : "-fx-background-color:red");
                    }
                }
            };
            return cell;
        });

        ttv.getColumns().setAll(symColumn, expiryColumn,pnlColumn);

        Scene scene = new Scene(ttv, 500, 500);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    private static class ClosedTradesPnL{
        private SimpleStringProperty symbol = new SimpleStringProperty("symbol");
        private SimpleObjectProperty<Date> expiry_date = new SimpleObjectProperty<>(new Date(System.currentTimeMillis()));
        private SimpleDoubleProperty realized_PNL= new SimpleDoubleProperty(Math.random()-0.5);

        public String getSymbol() {return symbol.get();}
        public Date getExpiry_date() {return expiry_date.get();}
        public double getRealized_PNL() {return realized_PNL.get();}

    }
}
brian
  • 10,619
  • 4
  • 21
  • 79
  • Brain I have one Question, How can we know whether row it is children or root ? – jaya sankar Feb 06 '15 at 09:40
  • Read the [javadoc here.](http://docs.oracle.com/javafx/2/api/javafx/scene/control/TreeItem.html#getParent()) If you still have questions you're supposed to ask a new question instead of using the comments. – brian Feb 06 '15 at 12:36
  • I've looking for a similar solution,, but my application is styled from .css. The style of the row is hardcoded in this example. – lg.lindstrom Jun 23 '16 at 13:17
  • @lg.lindstrom You could use ttr.getStyleClass.add("selector"), and also remove the style class when not needed. A better way would be to use [PseudoClass](https://docs.oracle.com/javase/8/javafx/api/javafx/css/PseudoClass.html), but that's another question. – brian Jun 23 '16 at 14:24