问题描述:

I have an object Contract and it contains Summary and Observable List of another object ContractDetails inside it.

Now, I am using ContractDetails to populate in tableview from Contract object.

I have a save button, which on clicking needs to save Contract along with ContractDetails. I am able to access ContractDetails since they are in tableview.

How do I access Contract properties in eventlistener of save button.

The related code is given below

 public class Contract {

private String tradeDate;

private String contractNote;

.....

.....

private String brokerId;

private ObservableList<ContractDetails> contractdetails = FXCollections.observableArrayList();

public Contract() {

}

public Contract(String tradeDate, String contractNote, ....., String brokerId,ObservableList<ContractDetails> contractdetails) {

this.tradeDate = tradeDate;

this.contractNote = contractNote;

....

....

this.contractdetails=contractdetails;

}

public String getTradeDate() {

return tradeDate;

}

public void setTradeDate(String tradeDate) {

this.tradeDate = tradeDate;

}

public String getContractNote() {

return contractNote;

}

public void setContractNote(String contractNote) {

this.contractNote = contractNote;

}

....

....

public ObservableList<ContractDetails> getContractdetails() {

return contractdetails;

}

public void setContractdetails(ObservableList<ContractDetails> contractdetails) {

this.contractdetails = contractdetails;

}

}

public class ContractDetails {

private String orderNo;

private String contractType;

private String symbol;

private String buysell;

private Integer quantity;

private Double buysellprice;

private Double netcontractValue;

public ContractDetails() {

}

public ContractDetails(String orderNo, String contractType, String symbol, String buysell, Integer quantity, Double buysellprice, Double netcontractValue) {

this.orderNo = orderNo;

this.symbol = symbol;

this.buysell = buysell;

this.quantity = quantity;

this.buysellprice = buysellprice;

this.netcontractValue = netcontractValue;

}

public String getOrderNo() {

return orderNo;

}

public void setOrderNo(String orderNo) {

this.orderNo = orderNo;

}

....

....

public Double getNetcontractValue() {

return netcontractValue;

}

public void setNetcontractValue(Double netcontractValue) {

this.netcontractValue = netcontractValue;

}

}

In the controller

==================

public class ContractViewController implements Initializable {

@FXML

private TableView<ContractDetails> tblcontractfx;

@FXML

private TableColumn<ContractDetails, String> contractTypefx;

@FXML

private TableColumn<ContractDetails, String> symbolfx;

....

....

@FXML

private Button savefx;

@FXML

private TextField txtclientcodefx;

@FXML

private TextField txttradedtfx;

private void fetchContracts(TableView tableView, Contract contract)

{ txttradedtfx.setText(contract.getTradeDate());

txtclientcodefx.setText(contract.getClientCode());

symbolfx.setCellValueFactory(new PropertyValueFactory<ContractDetails, String>("symbol"));

contractTypefx.setCellValueFactory(new PropertyValueFactory<ContractDetails, String>("contractType"));

tableView.setItems((ObservableList) contract.getContractdetails());

@FXML

private void saveClicked(ActionEvent event) { DBConnection DBcon = new DBConnection();

//Now I am getting the contract details from tableview tblcontractfx

ObservableList<ContractDetails> contractdetails = tblcontractfx.getItems();

//How do I get the summary values from contract. I am able to get those which are in text fields like txttradedtfx and txtclientcodefx.However contractNote which I am not using, I still need to retrieve it to populate into database.

String clientCode=txtclientcodefx.getText();

Thanks

网友答案:

Just store the contract in a local variable.

Contract contract;
private void fetchContracts(TableView tableView, Contract contract)
{  
this.contract = contract;
...
}


private void saveClicked(ActionEvent event) {  
// here you have full access to the contract variable
String contractNote = contract.getContractNote();
}

As an alternative, if you insist on combining it all in a single table, you could put the Contract into the table via setUserData and retrieve it via getUserData.

By the way, I still don't get your code. Why is there a tableView parameter when you have full access to TableView<ContractDetails> tblcontractfx

相关阅读:
Top