radio button show values in combo box

最后都变了- 提交于 2019-12-25 03:31:07

问题


I confused with this if-else because I'm new in Java & MySQL and I tried to make it by myself.

public Menu() {
        initComponents();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/restaurant", "root", "");
            System.out.println("ODBC Connection Successful");

            showCategory();

        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("ODBC Connection Failed" + e);

        }
    }

if - else

private void showCategory() {
        try {
            Statement stmt;
            stmt = con.createStatement();

            if (rbMFood.isSelected()) {
            ResultSet rs = stmt.executeQuery("SELECT * FROM menu_cat WHERE type_id = 'TY02'");
            while (rs.next()) {
                cmbMCat.addItem(rs.getString("menu_cat"));
            }

        } else {
            ResultSet rs = stmt.executeQuery("SELECT * FROM menu_cat WHERE type_id = 'TY01'");
            while (rs.next()) {
                cmbMCat.addItem(rs.getString("menu_cat"));
            }

            }
        } catch (Exception e) {

        }
    }

Radio Button

private void rbMFoodActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        type = "Food";
    }                                       

    private void rbMDrinkActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        type = "Drink";
    }

And I declare the function in the end of the program

private String type;

When I click drink / food, the category still drink's category.

The database

Any help would be greatly appreciated!


回答1:


You are using rs.getString("menu_cat") But the format is rs.getString(<Column Name>) But you are using rs.getString(<Table Name>) As "menu_cat" is name of the table and not the name of the column.

AFTER POSTING CONSTRUCTOR

What I see from the code you have posted, is that You have called showCategory() in the constructor. This method is responsible for populating the JComboBox cmbMCat. Now the cmbMCat is being populated when you are creating the new Menu. After that the items list of the cmbMCat does not change.

So, What I suggest is that you call the showCategory() from the rbMFoodActionPerformed and rbMDrinkActionPerformed methods. I hope this will solve your problem.

Also add cmbMCat.removeAllItems() before Statement stmt; to remove all the items that are there in the cmbMCat and reset it with a fresh list of items.

After comment regarding the if-else

Change the showCatagory() as below:

private void showCategory() {
        cmbMCat.removeAllItems();
        try {
            PreparedStatement stmt; //Used Prepared statement
            String sql = "SELECT * FROM menu_cat WHERE type_id = ?";
            stmt = con.prepareStatement(sql);

            if (type.equals("Drink")) {
                stmt.setString("TY01");    
            } else {
                stmt.setString("TY02");
            }
                ResultSet rs = stmt.executeQuery();
                while (rs.next()) {
                    cmbMCat.addItem(rs.getString("menu_cat"));
                }

            }
        } catch (Exception e) {

        }
    }

Also note that you eventListener code should be:

private void rbMFoodActionPerformed(java.awt.event.ActionEvent evt){                                        
    type = "Food";
    showCategory();
}                                       

private void rbMDrinkActionPerformed(java.awt.event.ActionEvent evt){                                         
    type = "Drink";
    showCategory();
}


来源:https://stackoverflow.com/questions/30050836/radio-button-show-values-in-combo-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!