Horizontal and Vertical control on button group

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 135
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster

Horizontal and Vertical control on button group

 
0
  #1
Nov 1st, 2009
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:-
  1. Questions Answers
  2. 1 radiobtn1 radiobtn2 radiobtn3
  3. 2 radiobtn1 radiobtn2 radiobtn3
  4. 3 radiobtn1 radiobtn2 radiobtn3
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 :-
  1. Questions Answers
  2. 1 radiobtn1 radiobtn2 checkbox3
  3. 2 radiobtn1 radiobtn2 checkbox3
  4. 3 radiobtn1 radiobtn2 checkbox3

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.table.*;
  5. import javax.swing.JRadioButton;
  6. import javax.swing.JButton;
  7.  
  8. public class JRadioButtonTableExample2 extends javax.swing.JFrame {
  9.  
  10. public JRadioButtonTableExample2() {
  11. super("JRadioButtonTable Example");
  12. DefaultTableModel dm = new DefaultTableModel(new Object[][]{
  13. {new String("Andrew")},
  14. {new String("Bolon")},
  15. {new String("Peter")},
  16. {new String("Mathew")},
  17. {new String("Austin")}},
  18. new Object[]{"Question", "Answer"});
  19. JTable table = new JTable(dm);
  20. String[] answer = {"A", "B", "C"};
  21. table.getColumn("Answer").setCellRenderer(new RadioButtonRenderer(answer));
  22. table.getColumn("Answer").setCellEditor(new RadioButtonEditor(new RadioButtonPanel(answer)));
  23. JButton myButton = new JButton("MyQuestion");
  24.  
  25. setContentPane(new JScrollPane(table));
  26. }
  27.  
  28. // Cell base
  29. class RadioButtonPanel extends JPanel {
  30. private JRadioButton[] buttons;
  31. private JRadioButton noSelectionButton;
  32.  
  33. RadioButtonPanel(String[] str) {
  34. setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  35. buttons = new JRadioButton[str.length];
  36. ButtonGroup group = new ButtonGroup();
  37. noSelectionButton = new JRadioButton();
  38. group.add(noSelectionButton);
  39. for (int i = 0; i < buttons.length; i++) {
  40. buttons[i] = new JRadioButton(str[i]);
  41. buttons[i].setFocusPainted(false);
  42. add(buttons[i]);
  43. group.add(buttons[i]);
  44. }
  45. }
  46.  
  47. public void addActionListener(ActionListener anActionListener) {
  48. for (int i = 0; i < buttons.length; i++) {
  49. buttons[i].addActionListener(anActionListener);
  50. }
  51. }
  52.  
  53. public void removeActionListener(ActionListener anActionListener) {
  54. for (int i = 0; i < buttons.length; i++) {
  55. buttons[i].removeActionListener(anActionListener);
  56. }
  57. }
  58.  
  59. public void setSelectedIndex(int index) {
  60. if (index < 0 || index >= buttons.length) {
  61. noSelectionButton.setSelected(true);
  62. return;
  63. }
  64. for (int i = 0; i < buttons.length; i++) {
  65. if (i == index) {
  66. buttons[i].setSelected(true);
  67. return;
  68. }
  69. }
  70. }
  71.  
  72. public int getSelectedIndex() {
  73. for (int i = 0; i < buttons.length; i++) {
  74. if (buttons[i].isSelected()) {
  75. return i;
  76. }
  77. }
  78. return -1;
  79. }
  80.  
  81. public JRadioButton[] getButtons() {
  82. return buttons;
  83. }
  84. }
  85.  
  86. class RadioButtonRenderer extends RadioButtonPanel implements TableCellRenderer {
  87. RadioButtonRenderer(String[] strs) {
  88. super(strs);
  89. }
  90.  
  91. public Component getTableCellRendererComponent(JTable table,
  92. Object value, boolean isSelected, boolean hasFocus, int row,
  93. int column) {
  94. if (value instanceof Integer) {
  95. setSelectedIndex(((Integer)value).intValue());
  96. }
  97. return this;
  98. }
  99. }
  100.  
  101. class RadioButtonEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
  102. private RadioButtonPanel theRadioButtonPanel;
  103.  
  104. public RadioButtonEditor(RadioButtonPanel aRadioButtonPanel) {
  105. theRadioButtonPanel = aRadioButtonPanel;
  106. theRadioButtonPanel.addActionListener(this);
  107. }
  108.  
  109. public Component getTableCellEditorComponent(JTable table,
  110. Object value, boolean isSelected, int row, int column) {
  111. if (value instanceof Integer) {
  112. theRadioButtonPanel.setSelectedIndex(((Integer) value).intValue());
  113. }
  114. return theRadioButtonPanel;
  115. }
  116.  
  117. public Object getCellEditorValue() {
  118. return new Integer(theRadioButtonPanel.getSelectedIndex());
  119. }
  120.  
  121. public void actionPerformed(ActionEvent e) {
  122. fireEditingStopped();
  123. }
  124. }
  125.  
  126. public static void main(String[] args) {
  127. JRadioButtonTableExample2 frame = new JRadioButtonTableExample2();
  128. frame.addWindowListener(new WindowAdapter() {
  129. public void windowClosing(WindowEvent e) {
  130. System.exit(0);
  131. }
  132. });
  133. frame.setSize(230, 140);
  134. frame.setVisible(true);
  135. }
  136. }

Thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,244
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 492
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
-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
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC