0

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
enjeru
  • 55
  • 1
  • 9
  • It's unclear what you are asking. – MadProgrammer May 07 '15 at 04:26
  • Sorry ._. I want to ask, what setter must I use for radio button? I already read your [answer](http://stackoverflow.com/questions/18260801/radio-buttons-and-setselected-or-something-else) before. But I still dont get it. @MadProgrammer – enjeru May 07 '15 at 04:30
  • What information from the database table are you using to store the state of the radio button? How would you determine if it needs to be selected or not? – MadProgrammer May 07 '15 at 04:34
  • I already upload the picture. If I write avocado, the radio button must select to food and category going to sushi. But that not working bcs I dunno what setter must I write. Do you get it? ._. @MadProgrammer – enjeru May 07 '15 at 04:38
  • `JRadioButton#setSelected(boolean)` is what you want. You might want to take a look at [How to Use Buttons, Check Boxes, and Radio Buttons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html). There must be something in you database which identifies the type of item it is. This is what I would use to determine which radio button to select – MadProgrammer May 07 '15 at 04:41
  • nah. I dunno what boolean it is ._. Can you help me to write the code? @MadProgrammer – enjeru May 07 '15 at 04:44
  • you can do it on Lost Focus of 'avocado' text box. if it match the string change the radio button's state as you want. @enjeru – Hardik Bharadava May 07 '15 at 04:44
  • You need to take a look at [Primitive Data Types](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html). Before you can set the state of the radio button, you need to have a means by which you can tell which radio button to use – MadProgrammer May 07 '15 at 04:48

3 Answers3

1

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);
prasad
  • 1,277
  • 2
  • 16
  • 39
  • Sorry. Which one that not clear? I still confused what that's true or false ._. – enjeru May 07 '15 at 04:46
  • @enjeru It's a `boolean` value, like `int`, but it can only hold `true` or `false` (yes or no). When you use `if (rbMFood.isSelected()) {`, `isSelected` is returning a `boolean` value of `true` or `false`. – MadProgrammer May 07 '15 at 04:47
0

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) {
    }
}
ELITE
  • 5,815
  • 3
  • 19
  • 29
0

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I use `if ("Food".equals(type)) {rbMFood.setSelected(true);}` and it's work. But combo box still empty ._. – enjeru May 07 '15 at 04:59
  • Then you need to call `showCategory` AFTER you have set the `JRadioButton` states... – MadProgrammer May 07 '15 at 05:01
  • how to write the code? hehe. I already write `showCategory();` after that but didn't work. – enjeru May 07 '15 at 05:04
  • All have to go be is a couple of out of context code snippets. Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer May 07 '15 at 05:05