Java,Mysql:如何基于另一个组合连接依赖的组合框

时间:2022-07-12 14:13:54

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

我正在寻找如何在第一个组合框上执行ActionPerformed以过滤下一个组合框。这是基于Mysql。我正在使用以下代码填充第一个组合,这是正常工作

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:

然后,我在第一个组合的ActionPerformed中添加另一个类似的代码来过滤第二个组合:

    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.

我注意到follogwing代码“System.out.println(rs2.getString(”T_AdresseCir“));”返回预期的结果,但不是组合框。还是空的。请帮助,谢谢。

1 个解决方案

#1


0  

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.

在你的actionPerformed方法中,你正在创建新的JComboBox但是你没有将它添加到gui。这可能就是为什么你看不到它的内容。您应该创建新的ComboBoxModel并将其设置在现有的JComboBox上。您应该使用try资源来使代码更具可读性。

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.

#1


0  

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.

在你的actionPerformed方法中,你正在创建新的JComboBox但是你没有将它添加到gui。这可能就是为什么你看不到它的内容。您应该创建新的ComboBoxModel并将其设置在现有的JComboBox上。您应该使用try资源来使代码更具可读性。

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.