Search code examples
javamysqlvectorjcombobox

Java, Mysql: how to connect dependent combobox based on another one


I'm seeking how to execute ActionPerformed on the first combobox in order to filter the next one. This is based on Mysql. I'm using the following codes to fill the first combo, which is working fine

Vector<String> comboBoxItems = new Vector<String>();
final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(comboBoxItems);
        try {
            new MconnectionDB();
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        PreparedStatement st1 = null;
        ResultSet rs1=null;
        String strPro = "";

        String sql1 ="select distinct T_AdressePro from t_adresse order by T_AdressePro";

        try {
            st1= MconnectionDB.con.prepareStatement(sql1);
            rs1 = st1.executeQuery();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }

        try {
            while (rs1.next()){
                strPro =rs1.getString("T_AdressePro");
                comboBoxItems.add(strPro);  
                comboBoxPro= new JComboBox<String>(model);  
            }

        } catch (SQLException e) {
        e.printStackTrace();
        }finally {
            try {
                rs1.close();
                st1.close();
                new MdeconnectionDB();
            }   
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

And then, I'm adding another similar code in ActionPerformed on the first combo to filter the second one:

    comboBoxPro.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {  
            Vector<String> comboBoxItemsC = new Vector<String>();
            final DefaultComboBoxModel<String> modelC = new DefaultComboBoxModel<String>(comboBoxItemsC);

            String strCir = "";
            PreparedStatement st2 = null;
            ResultSet rs2=null;

            String sql2 ="select distinct T_AdresseCir from t_adresse where T_AdressePro=? order by T_AdresseCir";

            try {
                new MconnectionDB();
            } catch (SQLException e2) {
                e2.printStackTrace();
            }
            comboBoxPro.getSelectedItem();
            strProCombo = comboBoxPro.getSelectedItem().toString();
            System.out.println(strProCombo);

            try {

                st2= MconnectionDB.con.prepareStatement(sql2);
                st2.setString(1, strProCombo); //strProCombo
                rs2 = st2.executeQuery();
            }catch (SQLException e1) {
                e1.printStackTrace();
            }

            try {
                while (rs2.next()){
                    System.out.println(rs2.getString("T_AdresseCir"));
                    strCir =rs2.getString("T_AdresseCir");
                    comboBoxItemsC.add(strCir); 
                    comboBoxCir= new JComboBox<String>(modelC); 
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    rs2.close();
                    st2.close();
                    new MdeconnectionDB();
                    }   
                    catch (SQLException e) {
                        e.printStackTrace();
                    }
            }
        }                   
});

I'm noticing that the follogwing code "System.out.println(rs2.getString("T_AdresseCir"));" is returning the expected result but not the combobox. Still empty. Your support please, thanks.


Solution

  • In your actionPerformed method you are creating new JComboBox but you don't add it to gui. That is probably why you can't see its contents. You should instead create new ComboBoxModel and set it on existing JComboBox. You should probably use try with resources too to make your code more readable.

    Pseudo code (I don't have your database):

    // create new model for your comboBox
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
    
    // fill model with data
    try (Connection con = /*get connection to your db by any means necessary*/;
        PreparedStatement stmt = con.prepareStatement(/*your query*/);
        ResultSet rs = stmt.executeQuery();) {
    
        while (rs.next()) {
            model.addElement(rs.getString(/*your column*/));
        }
    
        comboBox.setModel(model); // set model for your JComboBox
    
    } /*catch and all that*/
    // no need for finally because try-with-resources.