Introduction

The CheckboxGroup class is used to group the set of checkbox.

Class declaration

Following is the declaration for java.awt.CheckboxGroup class:

  1. public class CheckboxGroup
  2. extends Object
  3. implements Serializable

Class constructors

S.N.Constructor & Description
1 CheckboxGroup() () Creates a new instance of CheckboxGroup.

Class methods

S.N.Method & Description
1 Checkbox getCurrent() Deprecated. As of JDK version 1.1, replaced by getSelectedCheckbox().
2 Checkbox getSelectedCheckbox() Gets the current choice from this check box group.
3 void setCurrent(Checkbox box) Deprecated. As of JDK version 1.1, replaced by setSelectedCheckbox(Checkbox).
4 void setSelectedCheckbox(Checkbox box) Sets the currently selected check box in this group to be the specified check box.
5 StringtoString() Returns a string representation of this check box group, including the value of its current selection.

Methods inherited

This class inherits methods from the following classes:

  • java.lang.Object

CheckBoxGroup Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui > AwtControlDemo.java

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AwtControlDemo {
  7.  
  8. private Frame mainFrame;
  9. private Label headerLabel;
  10. private Label statusLabel;
  11. private Panel controlPanel;
  12.  
  13. public AwtControlDemo(){
  14. prepareGUI();
  15. }
  16.  
  17. public static void main(String[] args){
  18. AwtControlDemo awtControlDemo = new AwtControlDemo();
  19. awtControlDemo.showCheckBoxGroupDemo();
  20. }
  21.  
  22. private void prepareGUI(){
  23. mainFrame = new Frame("Java AWT Examples");
  24. mainFrame.setSize(400,400);
  25. mainFrame.setLayout(new GridLayout(3, 1));
  26. mainFrame.addWindowListener(new WindowAdapter() {
  27. public void windowClosing(WindowEvent windowEvent){
  28. System.exit(0);
  29. }
  30. });
  31. headerLabel = new Label();
  32. headerLabel.setAlignment(Label.CENTER);
  33. statusLabel = new Label();
  34. statusLabel.setAlignment(Label.CENTER);
  35. statusLabel.setSize(350,100);
  36.  
  37. controlPanel = new Panel();
  38. controlPanel.setLayout(new FlowLayout());
  39.  
  40. mainFrame.add(headerLabel);
  41. mainFrame.add(controlPanel);
  42. mainFrame.add(statusLabel);
  43. mainFrame.setVisible(true);
  44. }
  45.  
  46. private void showCheckBoxGroupDemo(){
  47.  
  48. headerLabel.setText("Control in action: CheckBoxGroup");
  49.  
  50. CheckboxGroup fruitGroup = new CheckboxGroup();
  51.  
  52. Checkbox chkApple = new Checkbox("Apple",fruitGroup,true);
  53. Checkbox chkMango = new Checkbox("Mango",fruitGroup,false);
  54. Checkbox chkPeer = new Checkbox("Peer",fruitGroup,false);
  55.  
  56. statusLabel.setText("Apple Checkbox: checked");
  57. chkApple.addItemListener(new ItemListener() {
  58. public void itemStateChanged(ItemEvent e) {
  59. statusLabel.setText("Apple Checkbox: checked");
  60. }
  61. });
  62.  
  63. chkMango.addItemListener(new ItemListener() {
  64. public void itemStateChanged(ItemEvent e) {
  65. statusLabel.setText("Mango Checkbox: checked");
  66. }
  67. });
  68.  
  69. chkPeer.addItemListener(new ItemListener() {
  70. public void itemStateChanged(ItemEvent e) {
  71. statusLabel.setText("Peer Checkbox: checked");
  72. }
  73. });
  74.  
  75. controlPanel.add(chkApple);
  76. controlPanel.add(chkMango);
  77. controlPanel.add(chkPeer);
  78.  
  79. mainFrame.setVisible(true);
  80. }
  81. }

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

  1. D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java

If no error comes that means compilation is successful. Run the program using following command.

  1. D:\AWT>java com.tutorialspoint.gui.AwtControlDemo

Verify the following output

awt_checkboxgroup.jpg