AWT CheckBoxGroup Class

  • Post author:
  • Post category:AWT
  • Post comments:1 Comment
CheckBoxGroup Class

Introduction

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

Class declaration

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

public class CheckboxGroup
   extends Object
      implements Serializable

Class constructors

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

Class methods

S.N.Method & Description
1Checkbox getCurrent()Deprecated. As of JDK version 1.1, replaced by getSelectedCheckbox().
2Checkbox getSelectedCheckbox()Gets the current choice from this check box group.
3void setCurrent(Checkbox box)Deprecated. As of JDK version 1.1, replaced by setSelectedCheckbox(Checkbox).
4void setSelectedCheckbox(Checkbox box)Sets the currently selected check box in this group to be the specified check box.
5String toString()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 > adglob > gui >AwtControlDemo.java

package com.adglob.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showCheckBoxGroupDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showCheckBoxGroupDemo(){
    
      headerLabel.setText("Control in action: CheckBoxGroup"); 

      CheckboxGroup fruitGroup = new CheckboxGroup();

      Checkbox chkApple = new Checkbox("Apple",fruitGroup,true);
      Checkbox chkMango = new Checkbox("Mango",fruitGroup,false);
      Checkbox chkPeer = new Checkbox("Peer",fruitGroup,false);

      statusLabel.setText("Apple Checkbox: checked");
      chkApple.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {             
            statusLabel.setText("Apple Checkbox: checked");
         }
      });

      chkMango.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Mango Checkbox: checked");
         }
      });

      chkPeer.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Peer Checkbox: checked");
         }
      });

      controlPanel.add(chkApple);
      controlPanel.add(chkMango);   
      controlPanel.add(chkPeer);       

      mainFrame.setVisible(true);  
   }
}

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

D:\AWT>javac com\adglob\gui\AwtControlDemo.java

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

D:\AWT>java com.adglob.gui.AwtControlDemo

Verify the following output

Previous Page:-Click Here

This Post Has One Comment

Leave a Reply