944,155 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1727
  • Java RSS
Nov 6th, 2009
0

Group with both Jcheckbox and Jbutton

Expand Post »
Hi,

I am trying to create a button group which can give me both Jcheckboxes as well as Jbuttons. The problem is that I am creating a button group and not able to fit the checkbox criteria into it. Actually I want to create 2 radiobuttons followed by one checkbox

I mean something like like:-
Java Syntax (Toggle Plain Text)
  1. No Buttons
  2. 1 jbutton1 jbutton2 jcheckbox1
  3. 2 jbutton1 jbutton2 jcheckbox1
  4. 3 jbutton1 jbutton2 jcheckbox1

Here is how I am creating my button group:-

Java Syntax (Toggle Plain Text)
  1. private JCheckBox[] buttons;
  2. private JRadioButton noSelectionButton;
  3.  
  4. RadioButtonPanel(String[] str) {
  5. setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  6. buttons = new JCheckBox[str.length];
  7. ButtonGroup group = new ButtonGroup();
  8.  
  9. noSelectionButton = new JRadioButton();
  10. group.add(noSelectionButton);
  11. for (int i = 0; i < buttons.length; i++) {
  12. buttons[i] = new JCheckBox(str[i]);
  13. buttons[i].setFocusPainted(false);
  14. add(buttons[i]);
  15. group.add(buttons[i]);
  16. }
  17. }

I was trying to follow this example to fit my requirement but in this case I need to use CheckboxGroup whereas I am using ButtonGroup and since I am filling my buttongroup like this
Java Syntax (Toggle Plain Text)
  1. group.add(buttons[i])


It is creating me problems when I declare CheckboxGroup instead of ButtonGroup.

Here is the example I am talking about :-
http://www.apl.jhu.edu/~hall/CWP-Cha...apter13.5.html

Here is my full code
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 MixedButtons extends javax.swing.JFrame {
  9.  
  10. public MixedButtons() {
  11. super("JRadioButtonTable Example");
  12. DefaultTableModel dm = new DefaultTableModel(new Object[][]{
  13. {new String("one")},
  14. {new String("two")},
  15. {new String("three")},
  16. {new String("four")},
  17. {new String("five")}},
  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.  
  24. setContentPane(new JScrollPane(table));
  25. }
  26.  
  27. // Cell base
  28. class RadioButtonPanel extends JPanel {
  29. private JCheckBox[] buttons;
  30. private JRadioButton noSelectionButton;
  31.  
  32. RadioButtonPanel(String[] str) {
  33. setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  34. buttons = new JCheckBox[str.length];
  35. ButtonGroup group = new ButtonGroup();
  36.  
  37. noSelectionButton = new JRadioButton();
  38. group.add(noSelectionButton);
  39. for (int i = 0; i < buttons.length; i++) {
  40. buttons[i] = new JCheckBox(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 JCheckBox[] 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. MixedButtons frame = new MixedButtons();
  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
Last edited by Web_Sailor; Nov 6th, 2009 at 2:14 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
Web_Sailor is offline Offline
164 posts
since Jun 2009
Nov 6th, 2009
1
Re: Group with both Jcheckbox and Jbutton
Why not stick with the ButtonGroup? You can put both radio buttons and check boxes in the button group just fine. CheckboxGroup is for use with AWT Checkbox.

(By the way, you forgot
Java Syntax (Toggle Plain Text)
  1. add(noSelectionButton);
in your code above. You created the button but didn't add it to the panel.)
Last edited by Ezzaral; Nov 6th, 2009 at 6:36 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 7th, 2009
0
Re: Group with both Jcheckbox and Jbutton
Click to Expand / Collapse  Quote originally posted by Ezzaral ...
Why not stick with the ButtonGroup? You can put both radio buttons and check boxes in the button group just fine. CheckboxGroup is for use with AWT Checkbox.

(By the way, you forgot
Java Syntax (Toggle Plain Text)
  1. add(noSelectionButton);
in your code above. You created the button but didn't add it to the panel.)
Hi.. Ok. I am now using only ButtonGroup. Yes I was wrong CheckboxGroup is used in AWT. Now I modified my code and I am now creating jbuttons separately and jcheckbox separately but the problem now that is coming is I am not able to set the width of my column ? I tried out many examples but not able to make it work.

I have 3 columns in JTable Question, Answer and Clown as per my Object[] string. I am keeping my checkbox in Clown. Now the problem is that I am unable control the Clown width.I have set table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF). All the columns are coming of eqaul size. . Howto make it work ? Please check my code below.

Here is my code below:
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 MixedButtons extends javax.swing.JFrame {
  9.  
  10. public MixedButtons() {
  11. super("JRadioButtonTable Example");
  12. DefaultTableModel dm = new DefaultTableModel(new Object[][]{
  13. {new String("one")},
  14. {new String("two")},
  15. {new String("three")},
  16. {new String("four")},
  17. {new String("five")}},
  18. new Object[]{"Question", "Answer","Clown"});
  19. JTable table = new JTable(dm);
  20. table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  21. String[] answer = {"A", "B", "C"};
  22. String[] Clown = {"Yes"};
  23. table.getColumn("Answer").setCellRenderer(new RadioButtonRenderer(answer));
  24. table.getColumn("Answer").setCellEditor(new RadioButtonEditor(new RadioButtonPanel(answer)));
  25.  
  26. table.getColumn("Clown").setCellRenderer(new CheckBoxRenderer(Clown));
  27. table.getColumn("Clown").setCellEditor(new CheckBoxEditor(new CheckBoxPanel(Clown)));
  28.  
  29. setContentPane(new JScrollPane(table));
  30. }
  31.  
  32. // Cell base
  33. class RadioButtonPanel extends JPanel {
  34. private JRadioButton[] buttons;
  35. private JRadioButton noSelectionButton;
  36.  
  37. RadioButtonPanel(String[] str) {
  38. setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  39. buttons = new JRadioButton[str.length];
  40. ButtonGroup group = new ButtonGroup();
  41.  
  42. noSelectionButton = new JRadioButton();
  43. group.add(noSelectionButton);
  44. for (int i = 0; i < buttons.length; i++) {
  45. buttons[i] = new JRadioButton(str[i]);
  46. buttons[i].setFocusPainted(false);
  47. add(buttons[i]);
  48. add(noSelectionButton);
  49. group.add(buttons[i]);
  50. }
  51. }
  52.  
  53. public void addActionListener(ActionListener anActionListener) {
  54. for (int i = 0; i < buttons.length; i++) {
  55. buttons[i].addActionListener(anActionListener);
  56. }
  57. }
  58.  
  59. public void removeActionListener(ActionListener anActionListener) {
  60. for (int i = 0; i < buttons.length; i++) {
  61. buttons[i].removeActionListener(anActionListener);
  62. }
  63. }
  64.  
  65. public void setSelectedIndex(int index) {
  66. if (index < 0 || index >= buttons.length) {
  67. noSelectionButton.setSelected(true);
  68. return;
  69. }
  70. for (int i = 0; i < buttons.length; i++) {
  71. if (i == index) {
  72. buttons[i].setSelected(true);
  73. return;
  74. }
  75. }
  76. }
  77.  
  78. public int getSelectedIndex() {
  79. for (int i = 0; i < buttons.length; i++) {
  80. if (buttons[i].isSelected()) {
  81. return i;
  82. }
  83. }
  84. return -1;
  85. }
  86.  
  87. public JRadioButton[] getButtons() {
  88. return buttons;
  89. }
  90. }
  91.  
  92. class CheckBoxPanel extends JPanel {
  93. private JCheckBox[] buttons;
  94. private JRadioButton noSelectionButton;
  95.  
  96. CheckBoxPanel(String[] str) {
  97. setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  98. buttons = new JCheckBox[str.length];
  99. ButtonGroup group = new ButtonGroup();
  100.  
  101. //noSelectionButton = new JRadioButton();
  102. group.add(noSelectionButton);
  103. for (int i = 0; i < buttons.length; i++) {
  104. buttons[i] = new JCheckBox(str[i]);
  105. buttons[i].setFocusPainted(false);
  106. add(buttons[i]);
  107. //add(noSelectionButton);
  108. group.add(buttons[i]);
  109. }
  110. }
  111.  
  112. public void addActionListener(ActionListener anActionListener) {
  113. for (int i = 0; i < buttons.length; i++) {
  114. buttons[i].addActionListener(anActionListener);
  115. }
  116. }
  117.  
  118. public void removeActionListener(ActionListener anActionListener) {
  119. for (int i = 0; i < buttons.length; i++) {
  120. buttons[i].removeActionListener(anActionListener);
  121. }
  122. }
  123.  
  124. public void setSelectedIndex(int index) {
  125. if (index < 0 || index >= buttons.length) {
  126. noSelectionButton.setSelected(true);
  127. return;
  128. }
  129. for (int i = 0; i < buttons.length; i++) {
  130. if (i == index) {
  131. buttons[i].setSelected(true);
  132. return;
  133. }
  134. }
  135. }
  136.  
  137. public int getSelectedIndex() {
  138. for (int i = 0; i < buttons.length; i++) {
  139. if (buttons[i].isSelected()) {
  140. return i;
  141. }
  142. }
  143. return -1;
  144. }
  145.  
  146. public JCheckBox[] getButtons() {
  147. return buttons;
  148. }
  149. }
  150.  
  151. class RadioButtonRenderer extends RadioButtonPanel implements TableCellRenderer {
  152. RadioButtonRenderer(String[] strs) {
  153. super(strs);
  154. }
  155.  
  156. public Component getTableCellRendererComponent(JTable table,
  157. Object value, boolean isSelected, boolean hasFocus, int row,
  158. int column) {
  159. if (value instanceof Integer) {
  160. setSelectedIndex(((Integer)value).intValue());
  161. }
  162. return this;
  163. }
  164. }
  165.  
  166. class CheckBoxRenderer extends CheckBoxPanel implements TableCellRenderer {
  167. CheckBoxRenderer(String[] strs) {
  168. super(strs);
  169. }
  170.  
  171. public Component getTableCellRendererComponent(JTable table,
  172. Object value, boolean isSelected, boolean hasFocus, int row,
  173. int column) {
  174. if (value instanceof Integer) {
  175. setSelectedIndex(((Integer)value).intValue());
  176. }
  177. return this;
  178. }
  179. }
  180.  
  181. class RadioButtonEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
  182. private RadioButtonPanel theRadioButtonPanel;
  183.  
  184. public RadioButtonEditor(RadioButtonPanel aRadioButtonPanel) {
  185. theRadioButtonPanel = aRadioButtonPanel;
  186. theRadioButtonPanel.addActionListener(this);
  187. }
  188.  
  189. public Component getTableCellEditorComponent(JTable table,
  190. Object value, boolean isSelected, int row, int column) {
  191. if (value instanceof Integer) {
  192. theRadioButtonPanel.setSelectedIndex(((Integer) value).intValue());
  193. }
  194. return theRadioButtonPanel;
  195. }
  196.  
  197. public Object getCellEditorValue() {
  198. return new Integer(theRadioButtonPanel.getSelectedIndex());
  199. }
  200.  
  201. public void actionPerformed(ActionEvent e) {
  202. fireEditingStopped();
  203. }
  204. }
  205.  
  206. class CheckBoxEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
  207. private CheckBoxPanel theRadioButtonPanel;
  208.  
  209. public CheckBoxEditor(CheckBoxPanel aRadioButtonPanel) {
  210. theRadioButtonPanel = aRadioButtonPanel;
  211. theRadioButtonPanel.addActionListener(this);
  212. }
  213.  
  214. public Component getTableCellEditorComponent(JTable table,
  215. Object value, boolean isSelected, int row, int column) {
  216. if (value instanceof Integer) {
  217. theRadioButtonPanel.setSelectedIndex(((Integer) value).intValue());
  218. }
  219. return theRadioButtonPanel;
  220. }
  221.  
  222. public Object getCellEditorValue() {
  223. return new Integer(theRadioButtonPanel.getSelectedIndex());
  224. }
  225.  
  226. public void actionPerformed(ActionEvent e) {
  227. fireEditingStopped();
  228. }
  229. }
  230.  
  231. public static void main(String[] args) {
  232. MixedButtons frame = new MixedButtons();
  233. frame.addWindowListener(new WindowAdapter() {
  234. public void windowClosing(WindowEvent e) {
  235. System.exit(0);
  236. }
  237. });
  238. frame.setSize(320, 140);
  239. frame.setVisible(true);
  240. }
  241. }

Thanks
Last edited by Web_Sailor; Nov 7th, 2009 at 7:14 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster
Web_Sailor is offline Offline
164 posts
since Jun 2009

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: P2P File sharing Application
Next Thread in Java Forum Timeline: Is calling Win32 API from Java possible??





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


Follow us on Twitter


© 2011 DaniWeb® LLC