两个相互关联的组合框[重复]

时间:2022-01-31 13:02:39

This question already has an answer here:

这个问题在这里已有答案:

I have two comboboxes ... First one shows the cities of hotels and the second one shows tha hotels name based on the city selected in first combo box. First Combobox is working correctly. second one shows is only for the first selected city. it does not make changes to itself after changing the city in first combo box ! what do I have to add to this code in order to be able to see changes in second combobox by changing the first combobox.

我有两个组合框......第一个显示酒店的城市,第二个显示根据第一个组合框中选择的城市的酒店名称。 First Combobox工作正常。第二个节目仅适用于第一个选定的城市。在第一个组合框中更改城市后,它不会对自身进行更改!我需要添加到此代码中,以便能够通过更改第一个组合框来查看第二个组合框中的更改。

    try
    {

        Class.forName("com.mysql.jdbc.Driver");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/JEREN","root","");
        Statement stat = con.createStatement();

        String City_Selected = jComboBox1.getSelectedItem().toString();           
        String select_HN = "select name from tbl_Hotel where city = '"+City_Selected+"';";

        ResultSet rs = stat.executeQuery(select_HN);



        while (rs.next())
        {              
            String hotel_name = rs.getString("name");
           // jLabel3.setText(hotel_name);
            jComboBox2.addItem(hotel_name);

         }//end while

        rs.close();
        stat.close();
        con.close();

         } catch (Exception e) {
              e.printStackTrace();
         } 

1 个解决方案

#1


0  

Like this ?

喜欢这个 ?

jComboBox1.addActionListener (new ActionListener () {
    public void actionPerformed(ActionEvent e) {
        jComboBox2.removeAllItems();
        fillComboBoxes();
    }
});

private void fillComboBoxes(){

 try
    {

        Class.forName("com.mysql.jdbc.Driver");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/JEREN","root","");
        Statement stat = con.createStatement();

        String City_Selected = jComboBox1.getSelectedItem().toString();           
        String select_HN = "select name from tbl_Hotel where city = '"+City_Selected+"';";

        ResultSet rs = stat.executeQuery(select_HN);



        while (rs.next())
        {              
            String hotel_name = rs.getString("name");
           // jLabel3.setText(hotel_name);
            jComboBox2.addItem(hotel_name);

         }//end while

        rs.close();
        stat.close();
        con.close();

         } catch (Exception e) {
              e.printStackTrace();
         } 

}

#1


0  

Like this ?

喜欢这个 ?

jComboBox1.addActionListener (new ActionListener () {
    public void actionPerformed(ActionEvent e) {
        jComboBox2.removeAllItems();
        fillComboBoxes();
    }
});

private void fillComboBoxes(){

 try
    {

        Class.forName("com.mysql.jdbc.Driver");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/JEREN","root","");
        Statement stat = con.createStatement();

        String City_Selected = jComboBox1.getSelectedItem().toString();           
        String select_HN = "select name from tbl_Hotel where city = '"+City_Selected+"';";

        ResultSet rs = stat.executeQuery(select_HN);



        while (rs.next())
        {              
            String hotel_name = rs.getString("name");
           // jLabel3.setText(hotel_name);
            jComboBox2.addItem(hotel_name);

         }//end while

        rs.close();
        stat.close();
        con.close();

         } catch (Exception e) {
              e.printStackTrace();
         } 

}