jay
jay

Reputation: 109

Getting TreeTableView working

I had a Tableview which I am trying to convert into TreeTableView. Getting it populated is not getting any easy for me. Here is the relevant code. How do we populate the TreeTable Column and can we dynamically populate TreeItem

From FXMLFile

<TreeTableView fx:id="openPositionsfx" prefHeight="200.0" prefWidth="900.0">
     <columns>
          <TreeTableColumn fx:id="symbolfx2" prefWidth="75.0" text="Symbol" />
          <TreeTableColumn fx:id="expirydatefx2" prefWidth="75.0" text="Expiry Date" />
          <TreeTableColumn fx:id="tradedatefx2" prefWidth="75.0" text="Trade Date" />
          <TreeTableColumn fx:id="buysellfx2" prefWidth="60.0" text="Buy/Sell" />
          <TreeTableColumn fx:id="quantityfx2" prefWidth="65.0" text="Quantity" />
          <TreeTableColumn fx:id="buysellPricefx2" prefWidth="65.0" text="Price" />
         <TreeTableColumn fx:id="mktpricefx2" prefWidth="70.0" text="Mkt Price" />
       <TreeTableColumn fx:id="daysopenfx2" prefWidth="70.0" text="Days Open" />
       <TreeTableColumn fx:id="unrealizedfx2" prefWidth="90.0" text="Unrealized PnL" />

     </columns>
</TreeTableView>

Code From Controller

@FXML
private TreeTableColumn<OpenPositions, Date> expirydatefx2;
@FXML
private TreeTableColumn<OpenPositions, Date> tradedatefx2;
@FXML
private TreeTableColumn<OpenPositions, String> buysellfx2;
@FXML
private TreeTableColumn<OpenPositions, Integer> quantityfx2;
@FXML
private TreeTableColumn<OpenPositions, Double> buysellPricefx2;
@FXML
private TreeTableColumn<OpenPositions, String> optiontypefx2;
@FXML
   private TreeTableColumn<OpenPositions, Double> mktpricefx2;
@FXML
private TreeTableColumn<OpenPositions, Integer> daysopenfx2;
@FXML
private TreeTableColumn<OpenPositions, String> unrealizedfx2;
private TreeTableView<OpenPositions> openPositionsfx;

FormattedData FD = new FormattedData();
private ObservableList<OpenPositions> openPositionsdata;

  @Override
public void initialize(URL url, ResourceBundle rb) {
    fetchOpenPositions(openPositionsfx);
}

private void fetchOpenPositions(TreeTableView treetableView) {

    //Is this correct? Since I didnt define this in FXML. Scenebuilder does not have TreeItem and hence cant define in FXML file
    final TreeItem<String> FuturesNode = new TreeItem<>("Futures");
    final TreeItem<String> OptionsNode = new TreeItem<>("Options");
    final TreeItem<String> root = new TreeItem<>("NSE F&O");
    treetableView = new TreeTableView<>(root);
    root.setExpanded(true);   
    root.getChildren().setAll(FuturesNode, OptionsNode); 



    openPositionsdata = FXCollections.observableArrayList();
    DBConnection DBcon = new DBConnection();

    try {

        Connection con = DBcon.getConnection();


        String sqlstring = null;
        Statement st = con.createStatement();

        sqlstring = " SELECT SYMBOL,EXPIRY_DATE, TRADE_DATE,  \n"
                + " BUYSELL, QUANTITY,BUYSELLPRICE,MARKET_PRICE,UNREALISED_PNL,DAYS_OPEN
                + " FROM TB_POSITIONS\n"
                + "WHERE EXPIRY_DATE>'01-JAN-2015'\n"
                + "ORDER BY EXPIRY_DATE,SYMBOL,TRADE_DATE";

        ResultSet rs = st.executeQuery(sqlstring);

        while (rs.next()) {
            String symbol = rs.getString("SYMBOL");
            Date expiryDate = rs.getDate("EXPIRY_DATE");
            Double strike_price = rs.getDouble("STRIKE_PRICE");
            Date tradeDate = rs.getDate("TRADE_DATE");
            String buysell = rs.getString("BUYSELL");
            Integer quantity = rs.getInt("quantity");
            Double buysellprice = rs.getDouble("BUYSELLPRICE");
            Double closePrice = rs.getDouble("MARKET_PRICE");
            Double unrealisedPnl = rs.getDouble("UNREALISED_PNL");
            Integer daysOpen = rs.getInt("DAYS_OPEN");


           openPositionsdata.add(new OpenPositions(symbol, expiryDate, tradeDate, buysell, quantity, buysellprice, closePrice, 
                     unrealisedPnlStr, daysOpen));

        }

        //Does not work
        symbolfx2.setCellValueFactory(new Callback<CellDataFeatures<OpenPositions,String>,ObservableValue<String>>(){
        public ObservableValue<String> call(CellDataFeatures<OpenPositions, String> p) {
        return p.getValue().symbol;
        }
        });

        //Does not work

         symbolfx2.setCellValueFactory( param -> param.getValue().getValue().getSymbol());

         //Works if it is tableview

           expirydatefx2.setCellValueFactory(new PropertyValueFactory<>("expiryDate"));

           treetableView.getColumns().setAll(openPositionsdata);
        treetableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        rs.close();
        con.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

OpenPostions.java

public class OpenPositions  {

String symbol;
Date expiryDate; 
Date tradeDate; 
String buysell;
int quantity; 
Double buysellprice;
Double marketPrice;
String unrealisedPnl;
Integer daysOpen;


public OpenPositions(String symbol, Date expiryDate, Date tradeDate, String buysell, int quantity, Double buysellprice, 
        Double marketPrice, String unrealisedPnl,Integer daysOpen  ) {
    this.symbol = symbol;
    this.tradeDate = tradeDate;
    this.expiryDate = expiryDate;
    this.buysell = buysell;
    this.quantity = quantity;
    this.buysellprice = buysellprice;
    this.marketPrice=marketPrice;
    this.unrealisedPnl=unrealisedPnl;
    this.daysOpen=daysOpen;
    this.optionType=optionType;
    this.strikePrice=strikePrice;
    this.pctpnl=pctpnl;
    this.currentRate=currentRate;
    this.todayspnl=todayspnl;
    this.todaypctpnl=todaypctpnl;


}



public String getSymbol() {
    return symbol;
}

public void setSymbol(String symbol) {
    this.symbol = symbol;
}

Upvotes: 0

Views: 4622

Answers (1)

James_D
James_D

Reputation: 209319

In your fetchOpenPositions(...) method, you re-assign a new TreeTableView to the parameter (and then don't do anything with it in terms of putting it in the scene graph). So any additional configuration is on that new TreeTableView, not the one that was created by the FXML file.

Just replace

treetableView = new TreeTableView<>(root);

with

treetableView.setRoot(root);

Additionally, your cell value factory implementations look like they are returning a String, instead of an ObservableValue<String>. If your OpenPositions class defines a property accessor method (the recommended approach):

public StringProperty valueProperty() { ... }

then

symbolfx2.setCellValueFactory( param -> param.getValue().symbolProperty() );

should work. Otherwise you will need

symbolfx2.setCellValueFactory( param -> new ReadOnlyStringWrapper(param.getValue().getSymbol()));

Upvotes: 0

Related Questions