-1

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.

    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();
         } 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). Factor out the DB and just hard-code some strings to use instead. 2) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. 3) Please don't forget to add a '?' to questions! Some people do a search in the page for '?' and if none exists in the 'question' go directly to the next (actual) question in line. – Andrew Thompson Jan 02 '14 at 16:11
  • You can provide a [SSCCE](http://sscce.org) , it seems that your query has nothing to do here, so use hardcoded values. You need `addItemListener` – nachokk Jan 02 '14 at 16:12
  • You seem to be ignoring my advice, given you've replied to someone else since my comment. As such, I'm going to simply vote to close this waste of our time. – Andrew Thompson Jan 02 '14 at 16:29
  • I am not ignoring you :) but I got my answer down here. thank you so much for your comments :) – Jeren Akhoundi Jan 02 '14 at 16:42

1 Answers1

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

}
user3152069
  • 408
  • 3
  • 9