根据从数据库中检索到的文本选择 jButton

Selecting jButton based on the text retrieved from the Data Base

关于在 netbeans 中将数据库用于程序,我又遇到了问题。这是我的问题,我有一个包含男性和女性按钮的按钮组。当您单击 'Submit' 按钮时,selected 按钮会将其文本写入 mySQL 数据库。所以问题是,我不知道它的反面。

我会尽可能清楚地说明我的问题。我想从数据库和 select 中检索文本,该文本在按钮组的一个按钮中检索文本。

例如,我从数据库中检索了'male',所以我想要一个代码为select 'male'单选按钮。谢谢。

编辑:

所以我现在明白了。我正在寻找一些方法,可以立即从数据库中检索到的文本 select 一个按钮。我用的是粗制滥造的过程,有点乱,但效果很好!

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

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

ButtonGroup 有一个 setSelected(...) 方法可以帮助您设置它包含的 JRadioButton 之一,并设置它的状态。例如在下面的代码中,我使用 JComboBox select JRadioButton 文本之一,然后使用 ButtonGroup 激活 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();
            }
        });
    }
}

我创建了一个小方法,允许我设置任何单选组按钮。如果您有很多单选按钮,并且不想使用 IF 来设置任何单选按钮,这非常方便。

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

然后

setButtonGroup(yourValue, yourButtonGroup.getElements());

也检查一下 setSelected a specific jradiobutton in a buttongroup based on action command