如何在ComboBox中选择从Database获取值的项目时显示文本字段

时间:2016-10-21 05:58:02

标签: javafx

@FXML
void tampilComboMilik(MouseEvent event) {
    String tmp = (String) cmb_milik.getSelectionModel().getSelectedItem();
    String select_qry = "SELECT * FROM tbl_kepemilikan WHERE nama =?";
    try {

        pst = conn.prepareStatement(select_qry);
        pst.setString(1, tmp);
        rs = pst.executeQuery();

            while(rs.next()){
            String add1 = rs.getString("id_milik");
            txt_warna.setText(add1);

            }

        rs.close();
        pst.close();            
    }catch(Exception e){
        System.out.println(e);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在fxml类中创建文本字段,并在initialize方法中将setVisible设置为False

@FXML
private String txt_warna;

@Override
    public void initialize(URL url, ResourceBundle rb) {
        txt_warna.setVisible(false); 
}

现在,当您点击此方法并获取值时,将其显示为

@FXML 
    void tampilComboMilik(MouseEvent event) {
    String tmp = (String) cmb_milik.getSelectionModel().getSelectedItem(); 
    String select_qry = "SELECT * FROM tbl_kepemilikan WHERE nama =?"; 
    try {
          pst = conn.prepareStatement(select_qry);
            pst.setString(1, tmp);
            rs = pst.executeQuery();

                while(rs.next()){
                String add1 = rs.getString("id_milik");
                txt_warna.setText(add1);
                txt_warna.setVisible(true);

                }

            rs.close();
            pst.close();            
        }catch(Exception e){
            System.out.println(e);
        }
    }