| | |
Horizontal and Vertical control on button group
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hi,
I am working on creating a jradiobutton group using JTable. Here is a brief definition of the problem.
My program gives an output like this:-
I am able to set horizontal control that only 1 radio button can be selected but how do I control vertically ?
As per my requirement I should allow only 1 radiobutton to be selected in a field. Even though horizontally I am doing that.
In short I mean both in rows and columns only 1 radiobutton should be selected.
Below is my code. What changes should I do ?
Also I wanted to use checkbox in for the 3rd button instead of radiobutton how can I do do that ? Since I am making a group of buttons here. Is it possible to do that ?
I mean somthing like this :-
Thanks
I am working on creating a jradiobutton group using JTable. Here is a brief definition of the problem.
My program gives an output like this:-
Java Syntax (Toggle Plain Text)
Questions Answers 1 radiobtn1 radiobtn2 radiobtn3 2 radiobtn1 radiobtn2 radiobtn3 3 radiobtn1 radiobtn2 radiobtn3
As per my requirement I should allow only 1 radiobutton to be selected in a field. Even though horizontally I am doing that.
In short I mean both in rows and columns only 1 radiobutton should be selected.
Below is my code. What changes should I do ?
Also I wanted to use checkbox in for the 3rd button instead of radiobutton how can I do do that ? Since I am making a group of buttons here. Is it possible to do that ?
I mean somthing like this :-
Java Syntax (Toggle Plain Text)
Questions Answers 1 radiobtn1 radiobtn2 checkbox3 2 radiobtn1 radiobtn2 checkbox3 3 radiobtn1 radiobtn2 checkbox3
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; import javax.swing.JRadioButton; import javax.swing.JButton; public class JRadioButtonTableExample2 extends javax.swing.JFrame { public JRadioButtonTableExample2() { super("JRadioButtonTable Example"); DefaultTableModel dm = new DefaultTableModel(new Object[][]{ {new String("Andrew")}, {new String("Bolon")}, {new String("Peter")}, {new String("Mathew")}, {new String("Austin")}}, new Object[]{"Question", "Answer"}); JTable table = new JTable(dm); String[] answer = {"A", "B", "C"}; table.getColumn("Answer").setCellRenderer(new RadioButtonRenderer(answer)); table.getColumn("Answer").setCellEditor(new RadioButtonEditor(new RadioButtonPanel(answer))); JButton myButton = new JButton("MyQuestion"); 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]); 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) { JRadioButtonTableExample2 frame = new JRadioButtonTableExample2(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setSize(230, 140); frame.setVisible(true); } }
Thanks
-1
#2 Nov 1st, 2009
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Other Threads in the Java Forum
- Previous Thread: tic-tac-toe!
- Next Thread: How to input and output from/or to a file?!
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api append apple applet application arguments array arrays automation bi binary bluetooth businessintelligence busy_handler(null) chat class classes client code component database draw eclipse encryption equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop main map method methods mobile netbeans newbie number open-source oracle oriented panel print problem program programming project qt recursion reference replaysolutions repositories return robot scanner screen scrollbar se server set singleton size sms socket sort sql string swing test threads time tree utility windows xor






