Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You haven't posted how you call your constructor, so maybe there is something there. I slightly modified your code, added a <code>main</code> method and it seems to work ok. Take a look at it:</p> <pre><code>import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Vector; import javax.swing.BoxLayout; import javax.swing.ButtonGroup; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.SwingUtilities; public class RadioDemo implements ActionListener { String buttonName; JPanel radioPanel = new JPanel(); ButtonGroup group = new ButtonGroup(); int result; JRadioButton birdButton[]; Vector&lt;String&gt; listName; private JRadioButton selectedButton; public RadioDemo(Vector&lt;String&gt; listName) { birdButton = new JRadioButton[listName.size()]; this.listName = listName; int i = 0; for (String buttonName : listName) { birdButton[i] = new JRadioButton(buttonName); if (i == 0) { birdButton[i].setSelected(true); selectedButton = birdButton[i]; } birdButton[i].setActionCommand(buttonName); group.add(birdButton[i]); birdButton[i].addActionListener(this); radioPanel.add(birdButton[i]); i++; } radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS)); // birdButton.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); result = JOptionPane.showConfirmDialog(null, radioPanel, "Please choose", JOptionPane.OK_CANCEL_OPTION); show(); } /** Listens to the radio buttons. */ @Override public void actionPerformed(ActionEvent e) { JRadioButton rb = (JRadioButton) e.getSource(); System.err.println(rb.getText() + " is selected"); selectedButton = rb; } public void show() { if (result == JOptionPane.OK_OPTION) { System.err.println(selectedButton.getText() + " is selected and approved"); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Vector&lt;String&gt; buttonNames = new Vector&lt;String&gt;(); buttonNames.add("Show"); buttonNames.add("Something"); buttonNames.add("Else"); buttonNames.add("Beep"); new RadioDemo(buttonNames); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload