Web_Sailor 0 Junior Poster

Hi,

I have a jRadiobuttongroup. I have 3 types of jradiobuttons in my group.

A     B     C
1     1    1
2     2    2
3     3    3
4     4    4

Now the problem is that when I selected suppose A type of buttons
some of the A type buttons get selected by themselves randomly even though i don't select them. I want to control it in the way that if I click on 3 in A type it should stay while I click the B suppose 2 but what happens is that randomly some buttons are getting selected which are creating a great amount of confusion.

What should I do to stop such a behaviour of getting randomly selected on their own ?

Here is the jRadiobuttongroup code:--

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.JRadioButton;
import javax.swing.JButton;
 
public class MixedButtons extends javax.swing.JFrame {
 
	public MixedButtons() {
		super("JRadioButtonTable Example");
		DefaultTableModel dm;
                dm =new DefaultTableModel(new Object[][]{},new Object[] {"Serial"});
                dm= new DefaultTableModel(new Object[][]{
                                 {new String("one")},
				 {new String("two")},
				 {new String("three")},
				 {new String("four")},
			         {new String("five")}},
		    new Object[]{"Question", "Answer"});
                //dm.insertRow(0, new Object[] { "r1" });
                //dm.addColumn("Serial");
		JTable table = new JTable(dm);
                //table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		String[] answer = {"A", "B", "C"};
                String[] keyword = {"Yes"};
	        table.getColumn("Answer").setCellRenderer(new RadioButtonRenderer(answer));
		table.getColumn("Answer").setCellEditor(new RadioButtonEditor(new RadioButtonPanel(answer)));
                

     
		setContentPane(new JScrollPane(table));
	}
 
	// Cell base
	class RadioButtonPanel extends JPanel {
		private JRadioButton[] buttons;
		private JRadioButton noSelectionButton;
 
		RadioButtonPanel(String[] str) {
			setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
			buttons = new JRadioButton[str.length];
			ButtonGroup group = new ButtonGroup();

			noSelectionButton = new JRadioButton();
			group.add(noSelectionButton);                       
			for (int i = 0; i < buttons.length; i++) {
				buttons[i] = new JRadioButton(str[i]);
				buttons[i].setFocusPainted(false);
				add(buttons[i]);
                                add(noSelectionButton);
				group.add(buttons[i]);
			}                         
		}
 
		public void addActionListener(ActionListener anActionListener) {
			for (int i = 0; i < buttons.length; i++) {
				buttons[i].addActionListener(anActionListener);
			}                              
		}
 
		public void removeActionListener(ActionListener anActionListener) {
			for (int i = 0; i < buttons.length; i++) {
				buttons[i].removeActionListener(anActionListener);
			}                          
		}
 
		public void setSelectedIndex(int index) {
			if (index < 0 || index >= buttons.length) {
				noSelectionButton.setSelected(true);
				return;
			}
			for (int i = 0; i < buttons.length; i++) {
				if (i == index) {
					buttons[i].setSelected(true);
					return;
				}
			}
		}
 
		public int getSelectedIndex() {
			for (int i = 0; i < buttons.length; i++) {
				if (buttons[i].isSelected()) {
					return i;
				}
			}
			return -1;
		}
 
		public JRadioButton[] getButtons() {
			return buttons;
		}
	}

 
	class RadioButtonRenderer extends RadioButtonPanel implements TableCellRenderer {
		RadioButtonRenderer(String[] strs) {
			super(strs);
		}
 
		public Component getTableCellRendererComponent(JTable table,
		 Object value, boolean isSelected, boolean hasFocus, int row,
			int column) {
			if (value instanceof Integer) {
				setSelectedIndex(((Integer)value).intValue());
			}
			return this;
		}
	}
       
 
	class RadioButtonEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
		private RadioButtonPanel theRadioButtonPanel;
 
		public RadioButtonEditor(RadioButtonPanel aRadioButtonPanel) {
			theRadioButtonPanel = aRadioButtonPanel;
			theRadioButtonPanel.addActionListener(this);
		}
 
		public Component getTableCellEditorComponent(JTable table,
		 Object value, boolean isSelected, int row, int column) {
			if (value instanceof Integer) {
				theRadioButtonPanel.setSelectedIndex(((Integer) value).intValue());
			}
			return theRadioButtonPanel;
		}
 
		public Object getCellEditorValue() {
			return new Integer(theRadioButtonPanel.getSelectedIndex());
		}
 
		public void actionPerformed(ActionEvent e) {
			fireEditingStopped();
		}
	}
        
 
	public static void main(String[] args) {
		MixedButtons frame = new MixedButtons();
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		frame.setSize(320, 140);
		frame.setVisible(true);
	}
}

Thanks