让TreeTableView正常工作

时间:2015-01-19 08:05:35

标签: javafx treetableview

我有一个Tableview,我试图将其转换为TreeTableView。填充它对我来说并不容易。这是相关的代码。我们如何填充TreeTable列,我们可以动态填充TreeItem

来自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>

控制器代码

@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;
}

1 个答案:

答案 0 :(得分:0)

fetchOpenPositions(...)方法中,您为参数重新分配了一个新的TreeTableView(然后在将其放入场景图中时不对其执行任何操作)。因此,任何其他配置都在新TreeTableView上,而不是由FXML文件创建的配置。

只需替换

treetableView = new TreeTableView<>(root);

treetableView.setRoot(root);

此外,您的单元格值工厂实现看起来像是返回String而不是ObservableValue<String>。如果您的OpenPositions类定义了属性访问器方法(推荐方法):

public StringProperty valueProperty() { ... }

然后

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

应该有效。否则你需要

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