Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Container remove method not working correctly
    text
    copied!<p>i hava added 1.TextArea 2.TextField then i start adding JButton successively on container...,now by using JRadioButton i want to remove JButton from container using this code </p> <pre><code>i=0; k=0; while(!birdButton[i].isSelected()){ i++; } System.out.println(i); k=i+2; list.removeElementAt(i); listName.removeElementAt(i); System.out.println(k); c.getContentPane().remove(k); </code></pre> <p>but when i select the 1st radiobutton 1st JButton should be deleted because of k=i+2; instead of deleting this one it deletes the TextArea(1st one). when i select the 3rd radiobutton then 1st JButton is deleted. can anyone let me know what the problem is? and also <code>System.out.println(i);</code> <code>System.out.println(k);</code> is not printing any value....here is the code</p> <pre><code>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, k; Vector&lt;String&gt; listName; Vector&lt;JComponent&gt; list; Container c; public RadioDemo(Vector&lt;String&gt; listName,Vector&lt;JComponent&gt; list,Container c) { birdButton=new JRadioButton[listName.size()]; this.listName=listName; this.c=c; this.list=list; i = 0; for (String buttonName : listName){ birdButton[i] = new JRadioButton(buttonName); birdButton[i].setActionCommand(buttonName); group.add(birdButton[i]); birdButton[i].addActionListener(this); radioPanel.add(birdButton[i]); i++; } birdButton[0].setSelected(true); 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; k = 0; while (!birdButton[i].isSelected()) { i++; } System.out.println(i); k = i + 2; list.removeElementAt(i); listName.removeElementAt(i); System.out.println(k); c.getContentPane().remove(k); c.getContentPane().validate(); // System.out.println(e.getActionCommand()); // c.getContentPane().rePaint(); } } } </code></pre>
 

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