单击javafx中的组合框时如何刷新数据?

时间:2019-06-03 16:09:10

标签: java javafx combobox

每次按Combobox时,我想从database给出的每个不同值中刷新数据

但是每次按下组合框时我得到的是,数据将继续增加而不是刷新

The Screenshot

public void initialize(URL url, ResourceBundle rb) {
        con = new DBConnect();
        con.connect();

        depart();
    }   
void init(Stage stage) {
        this.stage = stage;
    }

    public void depart(){
        String sql = "SELECT DISTINCT b.nama_bandara FROM departure as depart "
                + "INNER JOIN bandara as b on b.kode_bandara = depart.kode_bandara";
        try {
            pst = con.conn.prepareStatement(sql);
            con.rs = pst.executeQuery();

            while(con.rs.next()){
                combo.getItems().addAll(con.rs.getString(1));

            }
        } catch (SQLException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

每次单击时,数据总是增加

@FXML
    private void CB(ActionEvent event) {

        String depart = combo.getValue();
        String sql = "SELECT b.kota, bb.kota, single.harga FROM single_trip as single \n" +
                    "INNER JOIN departure as depart ON depart.kode_departure = single.kode_departure \n" +
                    "INNER JOIN bandara as b on b.kode_bandara = depart.kode_bandara \n" +
                    "INNER JOIN arrived as arrive on arrive.kode_arrived = single.kode_arrived \n" +
                    "INNER JOIN bandara as bb on bb.kode_bandara = arrive.kode_bandara \n" +
                    "WHERE b.nama_bandara = '"+depart+"'";
        try {
            con.st = con.conn.createStatement();
            con.rs = con.st.executeQuery(sql);
            Node[] nodes = new Node[1];

            while(con.rs.next()){

                for(Node node : nodes){

                    FXMLLoader hb = new FXMLLoader(getClass().getResource("ComboBoxloop.fxml"));
                    node = hb.load();
                    String namaDepart = con.rs.getString("b.kota");
                    String namaArrive = con.rs.getString("bb.kota");
                    int harga = con.rs.getInt("single.harga");
                    ComboBoxloopController controller = hb.getController();
                    controller.getValueFromCB(namaDepart, namaArrive, harga);

                    controller.setDepart(namaDepart);
                    controller.setArrive(namaArrive);
                    controller.setHarga(harga);

                   //i think i must add something in here
                   pnlComboBoxloop.getChildren().addAll(node);
                }
            }
        } catch (SQLException | IOException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

我认为我必须为此pnlComboBoxloop.getChildren().addAll(node);

因为每次单击组合框时数据总是会增加

public String getDepart() {
        return Depart;
    }

    public void setDepart(String Depart) {
        this.Depart = Depart;
    }

    public String getArrive() {
        return arrive;
    }

    public void setArrive(String arrive) {
        this.arrive = arrive;
    }

    public int getHarga() {
        return harga;
    }

    public void setHarga(int harga) {
        this.harga = harga;
    }

    public void getValueFromCB(String depart, String arrive, int harga){
               TripDeparture.setText(depart);
               TripArrived.setText(arrive);
               TripHarga.setText(Integer.toString(harga));
    }

1 个答案:

答案 0 :(得分:3)

您正在使用addAll-添加到列表中。 尝试改用setAll