質問

I have some problems with adding JRadioButton to ButtonGroup and then to JPanel, here is some code:

void modelsRadio () throws SQLException {

    JPanel modelsRadioPanel = new JPanel();

    Statement statement = db.setConnection();
    ResultSet rs = statement.executeQuery("SELECT * FROM МОДЕЛИ");
    ButtonGroup modelRadioGroup = new ButtonGroup();

    while (rs.next()) {

        modelsRadioPanel.add(new JRadioButton(rs.getString("НАЗВАНИЕ")));
        //modelRadioGroup.add(new JRadioButton(rs.getString("НАЗВАНИЕ")));
    }

    frame.add(modelsRadioPanel);
}

The idea is to get data from Oracle SQL Table and create radio's and put data to them, so, I can add them to ButtonGroup but can't add to JPanel. Or, if I don't add them to group and add them to JPanel I can't switch between them normally, they(radio buttons) works like a checkboxes.

役に立ちましたか?

解決 3

Hmm, I solve it like this:

void modelsRadio () throws SQLException {

    JPanel modelsRadioPanel = new JPanel();

    Statement statement = db.setConnection();
    ResultSet rs = statement.executeQuery("SELECT * FROM МОДЕЛИ");
    ButtonGroup modelRadioGroup = new ButtonGroup();

    while (rs.next()) {

       JRadioButton jr = new JRadioButton(rs.getString("НАЗВАНИЕ"));
       //modelRadioGroup.add(new JRadioButton(rs.getString("НАЗВАНИЕ")));
       modelRadioGroup.add(jr);
       modelsRadioPanel.add(jr);   
    }

    frame.add(modelsRadioPanel);
}

他のヒント

You need to add each radio button to the panel and button group as:

void modelsRadio () throws SQLException {

        JPanel modelsRadioPanel = new JPanel();

        Statement statement = db.setConnection();
        ResultSet rs = statement.executeQuery("SELECT * FROM МОДЕЛИ");
        ButtonGroup modelRadioGroup = new ButtonGroup();

        while (rs.next()) {
           JRadioButton jRadioButton =new JRadioButton(rs.getString("НАЗВАНИЕ")));

          //Add radio button to the panel
          modelsRadioPanel.add(jRadioButton);

          //Add radio button to the button group
          modelRadioGroup.add(jRadioButton);

          //Same for the remaining JRadioButton's 

        }

       // No need to add the button group to the panel

        frame.add(modelsRadioPanel);
    }

As you create buttons add them both to the ButtonGroup and to the panel. Make sure the same instance of a radio button goes both into the panel and the button group. In your code you create one instance for the panel and one instance for the group. Here is a basic example:

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

public class TestRadio {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("TestRadio");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JPanel panel = new JPanel();
                ButtonGroup modelRadioGroup = new ButtonGroup();

                for (int i = 0; i < 5; i++) {
                    JRadioButton b1 = new JRadioButton("Radio" + i);

                    modelRadioGroup.add(b1);
                    panel.add(b1);
                }

                f.add(panel);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }
}

See How to Use the ButtonGroup Component for details.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top