让 TreeTableView 工作
Getting TreeTableView working
我有一个 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;
}
在您的 fetchOpenPositions(...)
方法中,您将新的 TreeTableView
重新分配给参数(然后在将其放入场景图中时不对其进行任何操作)。因此,任何其他配置都在那个新 TreeTableView
上,而不是 FXML 文件创建的那个上。
只需更换
treetableView = new TreeTableView<>(root);
和
treetableView.setRoot(root);
此外,您的单元值工厂实现看起来像是在返回 String
,而不是 ObservableValue<String>
。如果您的 OpenPositions
class 定义了 属性 访问器方法(推荐方法):
public StringProperty valueProperty() { ... }
然后
symbolfx2.setCellValueFactory( param -> param.getValue().symbolProperty() );
应该可以。否则你将需要
symbolfx2.setCellValueFactory( param -> new ReadOnlyStringWrapper(param.getValue().getSymbol()));
我有一个 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;
}
在您的 fetchOpenPositions(...)
方法中,您将新的 TreeTableView
重新分配给参数(然后在将其放入场景图中时不对其进行任何操作)。因此,任何其他配置都在那个新 TreeTableView
上,而不是 FXML 文件创建的那个上。
只需更换
treetableView = new TreeTableView<>(root);
和
treetableView.setRoot(root);
此外,您的单元值工厂实现看起来像是在返回 String
,而不是 ObservableValue<String>
。如果您的 OpenPositions
class 定义了 属性 访问器方法(推荐方法):
public StringProperty valueProperty() { ... }
然后
symbolfx2.setCellValueFactory( param -> param.getValue().symbolProperty() );
应该可以。否则你将需要
symbolfx2.setCellValueFactory( param -> new ReadOnlyStringWrapper(param.getValue().getSymbol()));