| | |
Group with both Jcheckbox and Jbutton
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:-
Here is how I am creating my button group:-
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
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
Thanks
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)
No Buttons 1 jbutton1 jbutton2 jcheckbox1 2 jbutton1 jbutton2 jcheckbox1 3 jbutton1 jbutton2 jcheckbox1
Here is how I am creating my button group:-
Java Syntax (Toggle Plain Text)
private JCheckBox[] buttons; private JRadioButton noSelectionButton; RadioButtonPanel(String[] str) { setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); buttons = new JCheckBox[str.length]; ButtonGroup group = new ButtonGroup(); noSelectionButton = new JRadioButton(); group.add(noSelectionButton); for (int i = 0; i < buttons.length; i++) { buttons[i] = new JCheckBox(str[i]); buttons[i].setFocusPainted(false); add(buttons[i]); group.add(buttons[i]); } }
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)
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)
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 = new DefaultTableModel(new Object[][]{ {new String("one")}, {new String("two")}, {new String("three")}, {new String("four")}, {new String("five")}}, 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))); setContentPane(new JScrollPane(table)); } // Cell base class RadioButtonPanel extends JPanel { private JCheckBox[] buttons; private JRadioButton noSelectionButton; RadioButtonPanel(String[] str) { setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); buttons = new JCheckBox[str.length]; ButtonGroup group = new ButtonGroup(); noSelectionButton = new JRadioButton(); group.add(noSelectionButton); for (int i = 0; i < buttons.length; i++) { buttons[i] = new JCheckBox(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 JCheckBox[] 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(230, 140); frame.setVisible(true); } }
Thanks
Last edited by Web_Sailor; 30 Days Ago at 2:14 pm.
1
#2 30 Days Ago
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 in your code above. You created the button but didn't add it to the panel.)
(By the way, you forgot
Java Syntax (Toggle Plain Text)
add(noSelectionButton);
Last edited by Ezzaral; 30 Days Ago at 6:36 pm.
0
#3 30 Days Ago
•
•
•
•
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 forgotin your code above. You created the button but didn't add it to the panel.)Java Syntax (Toggle Plain Text)
add(noSelectionButton);
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)
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 = new DefaultTableModel(new Object[][]{ {new String("one")}, {new String("two")}, {new String("three")}, {new String("four")}, {new String("five")}}, new Object[]{"Question", "Answer","Clown"}); JTable table = new JTable(dm); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); String[] answer = {"A", "B", "C"}; String[] Clown = {"Yes"}; table.getColumn("Answer").setCellRenderer(new RadioButtonRenderer(answer)); table.getColumn("Answer").setCellEditor(new RadioButtonEditor(new RadioButtonPanel(answer))); table.getColumn("Clown").setCellRenderer(new CheckBoxRenderer(Clown)); table.getColumn("Clown").setCellEditor(new CheckBoxEditor(new CheckBoxPanel(Clown))); 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 CheckBoxPanel extends JPanel { private JCheckBox[] buttons; private JRadioButton noSelectionButton; CheckBoxPanel(String[] str) { setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); buttons = new JCheckBox[str.length]; ButtonGroup group = new ButtonGroup(); //noSelectionButton = new JRadioButton(); group.add(noSelectionButton); for (int i = 0; i < buttons.length; i++) { buttons[i] = new JCheckBox(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 JCheckBox[] 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 CheckBoxRenderer extends CheckBoxPanel implements TableCellRenderer { CheckBoxRenderer(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(); } } class CheckBoxEditor extends AbstractCellEditor implements TableCellEditor, ActionListener { private CheckBoxPanel theRadioButtonPanel; public CheckBoxEditor(CheckBoxPanel 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
Last edited by Web_Sailor; 30 Days Ago at 7:14 am.
![]() |
Similar Threads
- GUI calculations Help (Java)
- Pie chart help (Java)
- evaluate with netbean (Java)
- GUI and inheritance (Java)
- GUI and dynamic method binding (Java)
- creating a JOptionPane quiz (Java)
- JTextArea question (Java)
- Printing JPanel containing JLable, JTable, JButton, JTextPane, JTextArea (Java)
- Help with a reservation program! GUI Messed XD (Java)
Other Threads in the Java Forum
- Previous Thread: P2P File sharing Application
- Next Thread: Is calling Win32 API from Java possible??
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui health html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






