enjeru
enjeru

Reputation: 55

radio button use what setter?

My values get it from database (mysql).

If in text field, there will be like this : txtMID.setText("" + rs.getString("menu_id"));

Are in combo box like this? cmbMCat.setSelectedItem("" + rs.getString("menu_cat"));

How about radio button? I think that use if-else condition. But I don't get how to write the code.

My radio button set in the end of coding : private String type;

and here the button :

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

    }                                       

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

And this my showCategory();

private void showCategory() {
        try {
            cmbMCat.removeAllItems();
            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) {
        }

enter image description here

Please, I really need your help T__T Ask if my question make you confused because my bad english.

Upvotes: 0

Views: 674

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347264

When loading the record, you should have a value which can determine what type of data it is you are loading, for example type_id seems to be used based on which JRadioButton is selected.

You would extract this value from the ResultSet and either use a if-else statement or case statement to determine which radio button you want selected

String type = rs.getString("type_id")
switch (type) {
    case "TY01":
        rbMDrink.setSelected(true);
        break;
    case "TY02":
        rbMFood.setSelected(true);
        break;
    // Other possible cases...
}

This assumes that the JRadioButtons belong to the same ButtonGroup, so selecting one will deselect the other.

Take a closer look at How to Use Buttons, Check Boxes, and Radio Buttons for more details

Upvotes: 0

ELITE
ELITE

Reputation: 5940

i think you have to set the radio button selected according to database. for that thing use following logic.

private void showCategory() {
    try {
        cmbMCat.removeAllItems();
        Statement stmt;
        stmt = con.createStatement();
        switch(type) {
            case "Food":
            rbMFood.setSelected(true);
            break;
            case "Drink";
            rbMDrink.setSelected(true);
            break;
        }
        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) {
    }
}

Upvotes: 0

prasad
prasad

Reputation: 1302

Hi First of all your question is not clear. The below suggestions are of based on my understanding of your question.
On loading of your application you want to initializing you UI components based on the data coming form database(mysql).
So you will get one field like true or false about that radio button from database

if(true)
   rbmFood.setSelected(true);
else
   rbmDrink.setSelected(true);

Upvotes: 1

Related Questions