如何从javafx中的事件侦听器访问对象

时间:2015-02-04 17:56:47

标签: javafx event-listener

我有一个对象契约,它包含其中另一个对象ContractDetails的Summary和Observable List。

现在,我正在使用ContractDetails从Contract对象填充tableview。

我有一个保存按钮,点击时需要保存合同和ContractDetails。我可以访问ContractDetails,因为它们在tableview中。

如何在保存按钮的eventlistener中访问Contract属性。

相关代码如下:

    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();

由于

1 个答案:

答案 0 :(得分:0)

只需将合约存储在本地变量中。

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();
}

作为替代方案,如果您坚持将所有内容合并到一个表格中,则可以通过setUserData将合同放入表格并通过getUserData检索。

顺便说一下,我仍然没有得到你的代码。当您拥有tableView

的完全访问权限时,为什么会有TableView<ContractDetails> tblcontractfx参数