All About Checkbox in AWT in Java

0
21
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Checkbox in java
Checkbox in java

All About Checkbox in AWT in Java:

A checkbox is a graphical square box component provides an ‘ON'(true) and ‘OFF'(false) state. Clicking on the checkbox changes its state from ON to OFF or OFF to ON.When the checkbox is clicked,AWT send an action event to the checkbox.This event’s target is the checkbox and its object is a boolean,giving the new state to the checkbox.An application should override the action method of the checkbox or one of it’s containing window in order to cause some action to occur.
Unlike some other windows checkbox, it does not have a third grayed out state.
Optionally several checkboxes can be grouped together into a check box group.At most one button in a group can be in the ‘ON’ state at any given point of time.Pushing a checkbox to turn it ‘ON’ forces any other checkbox in the group(that is ‘ON’) to ‘OFF’ state.

Checkboxes operate independently to each other,we can check them or uncheck them as many time as we want.Checkbox also provides a textual label.

The class structure of the Checkbox is given as:


public class Checkbox extends java.awt.Component
                      implements java.awt.ItemSelectable{
//constructors
public Checkbox();//default constructor with no label.The checkbox is set to off 
//(by default) and 
//is not part of any checkbox group
public Checkbox(String label);//creates a check box with label as the value 
//given in the parameter
public Checkbox(String label,fencingType,boolean initialState);
public Checkbox(String label,boolean initialState);
public Checkbox(String label,boolean initialState,ChechboxGroup group);
public CheckBox(String label,ChechboxGroup group,boolean state);
//creates a checkbox with the specified label in the specified checkboxGroup 
//and set to the specified state.
//Methods
public void addNotify();
public String getLabel();
public boolean getState();//returns the state of the checkbox
protected String paramString();
public CheckBoxGroup getCheckboxgroup();
public void setCheckboxGroup(CheckboxGroup g);
public synchronized void setLabel(String label);//sets the label of the checkbox
public void setState(boolean state);//sets the state of the checkbox
public synchronized void addItemListener(ItemListener l);
public synchronized void removeItemListener(ItemListener l);
public Object[] getSelectedObjects();
public void setCheckboxGroup(CheckboxGroup group);
protected void processEvent(AWTEvent e);
protected void processItemEvent(ItemEvent e);

An Example:


Checkbox c3=new Checkbox("value",null,true);

How to work with Checkbox?


Checkbox cb=new Checkbox("Hello");
//then with the help of the above methods we can add ,remove items and retrieve items
 

If a user clicks on a checkbox, it gets registered in addItemListener() method.Post this we can receive notification in the item state changed event of the both checking and unchecking of the boxes.If we know which box caused the event notification,then we can ask whether the checkbox currently checked or not to decide what action should be taken.We can also execute the getStateChange() method on the itemEvent which will return DESELECTED or SELECTED.

Code Snippet to work on Checkbox:


public void itemStateChanged(ItemEvent iEvt)
{
int evType=IEvt.getStateChange();
if(evType==ItemEvent.SELECTED)
//do something
else
//do aomething
}
 
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here