如何根据 Java eclipse 中的另一个 JCombobox 填充 JCombobox

How To Populate JCombobox Depending In Another JCombobox In Java eclipse

来自比利时。

我已经花了 3 天时间尝试使用组合框在值之间填充。 我使用 Sqlite 并使用 4 tables 将数据保存在 table "Recipes"

因此,如果我选择来自 cmbCategory 的 "meat",来自 table "sbCategory",另一个组合框 "cmbDescription" 来自 table "Products"应该只显示与类别 "meat"

相关的内容

显示的代码正在运行我可以获取框的值。这个我贴在了底部 "fillCombo();"

我真的很绝望。 3 天搜索让这个工作,直到我找到这个网站。我希望你们能在这里帮助我。我很佩服你们的知识。我是一名正在尝试编写自己的应用程序的厨师。 先感谢您。我不能付钱给你们,但是如果你们在比利时,我可以提供一些咖啡..

// public void fillCombo(){

    try {
        String sql= "Select * from sbCategorie";
        String sql1= "Select * from Products";
        String sql2= "Select * from UnitsRecipe";
        String sql3= "Select * from Classification";
        PreparedStatement pst=connection.prepareStatement(sql);
        PreparedStatement pst1=connection.prepareStatement(sql1);
        PreparedStatement pst2=connection.prepareStatement(sql2);
        PreparedStatement pst3=connection.prepareStatement(sql3);
        ResultSet rs=pst.executeQuery();
        ResultSet rs1=pst1.executeQuery();
        ResultSet rs2=pst2.executeQuery();
        ResultSet rs3=pst3.executeQuery();
        while(rs.next()){
            BoxCategory.addItem(rs.getString("Categorie"));
            //BoxDescription.addItem(rs.getString("Description"));
        }
        while(rs1.next()){
            BoxDescription.addItem(rs1.getString("Description"));
        }
        while(rs2.next()){
            BoxUnit
            .addItem(rs2.getString("Unit"));
        }
        while(rs3.next()){
            BoxClassification
            .addItem(rs3.getString("Classification"));
        }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

如图所示

BoxCategory.addItemListener(新的 ItemListener() {

        public void itemStateChanged(ItemEvent arg0) {
            //
            //
            String s=BoxCategory.getSelectedItem().toString();
            String sql="Select * from Products where Category='"+s+"'";
            //
        try {
            PreparedStatement pst=connection.prepareStatement(sql);

            ResultSet rs=pst.executeQuery();
        while (rs.next()){
            //BoxDescription.removeAllItems();
            BoxCategory.setSelectedItem(rs.getString("Description"));

            }
            } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

能否请您替换以下代码片段:

BoxCategory.addItemListener(new ItemListener() {

    public void itemStateChanged(ItemEvent arg0) {
        //
        //
        String s = BoxCategory.getSelectedItem().toString();
        String sql = "Select * from Products where Category='" + s + "'";
        //
        try {
            PreparedStatement pst = connection.prepareStatement(sql);

            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                // BoxDescription.removeAllItems();
                BoxCategory.setSelectedItem(rs.getString("Description"));

            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});

通过以下代码片段:

BoxCategory.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent arg0) {
            String s = BoxCategory.getSelectedItem().toString();
            String sql = "Select * from Products where Category='" + s + "'";
            try {
                PreparedStatement pst = connection.prepareStatement(sql);
                ResultSet rs = pst.executeQuery();
                BoxDescription.removeAllItems();
                while (rs.next()) {
                    BoxDescription.addItem(rs.getString("Description"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
});

看看结果?