Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy JRadioButton is not selected even by using setSelected(true)
    primarykey
    data
    text
    <p>i only want one of the above button to be selected by default but setSelected(true) is not working . when i run the below program none of the JRadoiButton is selected</p> <pre><code>import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RadioDemo implements ActionListener { String buttonName; JPanel radioPanel=new JPanel(); ButtonGroup group = new ButtonGroup(); Enumeration enl; int result; ActionEvent e; JRadioButton birdButton[]; int i; Vector&lt;JComponent&gt; list; Vector&lt;String&gt; listName; public RadioDemo(Vector&lt;JComponent&gt; list,Vector&lt;String&gt; listName,Enumeration en,Enumeration enl) { birdButton=new JRadioButton[list.size()]; this.enl=enl; this.list=list; this.listName=listName; for(i=0;i&lt;list.size()-1;i++) { buttonName=(String)enl.nextElement(); birdButton[i] = new JRadioButton(buttonName); birdButton[i].setSelected(false); birdButton[i].setActionCommand(buttonName); group.add(birdButton[i]); birdButton[i].addActionListener(this); radioPanel.add(birdButton[i]); } buttonName=(String)enl.nextElement(); birdButton[i] = new JRadioButton(buttonName); birdButton[i].setSelected(true); birdButton[i].setActionCommand(buttonName); group.add(birdButton[i]); birdButton[i].addActionListener(this); radioPanel.add(birdButton[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. */ public void actionPerformed(ActionEvent e) { this.e=e; } public void show() { if (result == JOptionPane.OK_OPTION) { i=0; while(!birdButton[i].isSelected()) { i++; System.out.println(i); } //list.removeElementAt(i); //listName.removeElementAt(i); System.out.println(i); System.out.println(e.getActionCommand()); } } </code></pre> <p>i also try birdButton[0].setSelected(true); out of loop </p>
    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. 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