1

I have again a problem regarding on using database to a program in netbeans. This is my problem, I have a button group which contains male and female button. When you clicked the 'Submit' button, the selected button will write the text of it to mySQL data base. So the problem is, I don't know the reverse of it.

I will make my question as clear as possible. I want to retrieve a text from database and select that retrieved text in one of the buttons of button group.

For example, I retrieved 'male' from the data base, so I want a code to select 'male' radio button. Thank you.

EDIT:

So I got it now. I am looking for some method that will instantly select a button from a text retrieved in data base. I used the crude process, little messy but it worked nice!

String temp = (jTable1.getModel().getValueAt(row, 11).toString());

    if ("Male".equals(temp)) {
        maleRButton.setSelected(true);
    }
    else if ("Female".equals(temp)) {
        femaleRButton.setSelected(true);
    }

2 Answers2

1

ButtonGroup has a setSelected(...) method that would help you set one of the JRadioButtons that it contains, and set its state. For example in the code below, I use a JComboBox to select one of the JRadioButton texts, and then use the ButtonGroup to activate the selected String:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;    
import javax.swing.*;

public class SelectButton extends JPanel {
    private static final String[] TEXTS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
    private ButtonGroup buttonGroup = new ButtonGroup();
    private JComboBox<String> comboBox = new JComboBox<>(TEXTS);

    public SelectButton() {
        JPanel rBtnPanel = new JPanel(new GridLayout(1, 0));
        for (String text : TEXTS) {
            JRadioButton radioButton = new JRadioButton(text);
            radioButton.setActionCommand(text);
            rBtnPanel.add(radioButton);
            buttonGroup.add(radioButton);
        }

        comboBox.setSelectedIndex(-1);
        comboBox.addActionListener(new ComboListener());
        JPanel bottomPanel = new JPanel();
        bottomPanel.add(comboBox);

        setLayout(new BorderLayout());
        add(rBtnPanel, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.PAGE_END);
    }

    private class ComboListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String command = comboBox.getSelectedItem().toString();
            Enumeration<AbstractButton> elements = buttonGroup.getElements();
            while (elements.hasMoreElements()) {
                AbstractButton btn = elements.nextElement();
                if (btn.getActionCommand().equals(command)) {
                    buttonGroup.setSelected(btn.getModel(), true);
                }
            }

        }
    }

    private static void createAndShowGui() {
        SelectButton mainPanel = new SelectButton();

        JFrame frame = new JFrame("Main");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Nice sir, but I figured it out by using this statement: String temp = (jTable1.getModel().getValueAt(row, 11).toString()); if ("Male".equals(temp)) { maleRButton.setSelected(true); } else if ("Female".equals(temp)) { femaleRButton.setSelected(true); } – Mark Jhomel Olimon Nov 21 '15 at 13:40
  • @MarkJhomelOlimon: that will work, but my code will work with any number of JRadioButtons, does not require a hard-coded if / else-if block, and so is more flexible. – Hovercraft Full Of Eels Nov 21 '15 at 13:46
  • Yeah I had understand sir, and I will implement that in case I have more than 10 radio buttons. – Mark Jhomel Olimon Dec 11 '15 at 14:34
0

I created a small method that allow me to set any radio group button. Very convenient if you have many radio buttons, and you don't want to use an IF for set any radio button.

public void setButtonGroup(String rdValue, Enumeration elements ){
    while (elements.hasMoreElements()){
        AbstractButton button = (AbstractButton)elements.nextElement();
        if(button.getActionCommand()==rdValue){
            button.setSelected(true);
        }
    }
}

then

setButtonGroup(yourValue, yourButtonGroup.getElements());

also check setSelected a specific jradiobutton in a buttongroup based on action command

Community
  • 1
  • 1
Carlos
  • 87
  • 5