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

时间:2016-07-03 01:51:19

标签: java combobox jcombobox populate

来自比利时。

我现在3天试图用组合框填充值。 我使用Sqlite并使用4个表来保存表格中的数据" Recipes"

所以如果我选择让我们说"肉"来自cmbCategory,来自table" sbCategory",另一个组合框" cmbDescription"来自表"产品"应该只显示与类别相关的内容"肉类"

显示的代码正常工作我可以在框中获取值。这个我粘贴在底部" 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(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();
        }

1 个答案:

答案 0 :(得分:-1)

请您替换以下代码段:

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

并查看结果?