944,131 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1127
  • Java RSS
Nov 1st, 2009
0

Horizontal and Vertical control on button group

Expand Post »
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:-
Java Syntax (Toggle Plain Text)
  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 :-
Java Syntax (Toggle Plain Text)
  1. Questions Answers
  2. 1 radiobtn1 radiobtn2 checkbox3
  3. 2 radiobtn1 radiobtn2 checkbox3
  4. 3 radiobtn1 radiobtn2 checkbox3

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Junior Poster
Web_Sailor is offline Offline
164 posts
since Jun 2009
Nov 1st, 2009
-1
Re: Horizontal and Vertical control on button group
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: tic-tac-toe!
Next Thread in Java Forum Timeline: How to input and output from/or to a file?!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC