0

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;
}
jay
  • 101
  • 1
  • 3
  • 13

1 Answers1

0

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()));
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thanks James. Basically am not able compile this The error which I am getting is F:\MyProjects\MarketAnalytics\src\marketanalytics\PnlSummaryController.java:152: error: incompatible types: String cannot be converted to ObservableValue return p.getValue().symbol; java:150: error: incompatible types: ,ObservableValue>> cannot be converted to Callback,ObservableValue> – jay Jan 21 '15 at 05:49
  • See edits. It also looks like you have imported the wrong `CellDataFeatures` class (`TableColumn.CellDataFeatures` instead of `TreeTableColumn.CellDataFeatures`), but you should be able to use the code I showed anyway and let the compiler infer the types. If that doesn't work, update your question to show the `OpenPositions` class. – James_D Jan 21 '15 at 11:41
  • Progressed a bit today,but my inexperience in java is showing up. I have added OpenPositions.java to the question. Only have getter and setter methods in that class. Have tried with variations of code and finally this once compiles and runs. However the treetableview still shows blank. symbolfx2.setCellValueFactory(new TreeItemPropertyValueFactory<>("symbol")); treetableView.getColumns().setAll(symbolfx2,expirydatefx2,tradedatefx2,buysellfx2,buysellPricefx2,mktpricefx2,daysopenfx2,unrealizedfx2,pctpnlfx,strikepricefx2,optiontypefx2, quantityfx2,todayMTMfx,todaypctfx); – jay Jan 21 '15 at 18:06
  • This is how the blank screen is looking - http://i62.tinypic.com/ddm7gz.jpg Below versions compiles but errors out in runtime symbolfx2.setCellValueFactory( param -> new ReadOnlyStringWrapper(param.getValue().getValue().getSymbol())); symbolfx2.setCellValueFactory( (TreeTableColumn.CellDataFeatures param) -> new ReadOnlyStringWrapper(param.getValue().getValue().getSymbol()) ); – jay Jan 21 '15 at 18:07