Class Checkbox in AWT in Java

0
216
Spread the love
  • 29
  • 34
  • 56
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    166
    Shares

Class Checkbox in AWT in Java:

A checkbox is a graphical square box component that 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 sends 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 checkbox 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 times as we want. The checkbox also provides a textual label.

In short, The Checkbox is a Component that provides a true or false toggle switch for user input.

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,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);

The details of the class structure are given as follows:

public Checkbox();

public Checkbox() constructs a Checkbox object with no label that is initially false.

public Checkbox(String label);

public Checkbox(String label) constructs a Checkbox object with the given label that is initially false.

Parameter
label – Text to display with the Checkbox.

public Checkbox(String label, boolean state);

public Checkbox(String label, boolean state) constructs a Checkbox with the given label, initialized to the given state.

Parameter
label – Text to display with the Checkbox.
state – Initial value of the Checkbox.

public Checkbox (String label, boolean state, CheckboxGroup group);

public Checkbox (String label, boolean state, CheckboxGroup group) constructs a Checkbox with the given label, initialized to the given state and belonging to the group.

Parameter
label – Text to display with the Checkbox.
state – Initial value of the Checkbox.
group -The CheckboxGroup this Checkbox should belong to.

public Checkbox(String label, CheckboxGroup group, boolean state);

public Checkbox(String label, CheckboxGroup group, boolean state) constructs a Checkbox object with the given settings.

Parameter
label – Text to display with the Checkbox.
group – The CheckboxGroup this Checkbox should belong to.
state – Intial value of the Checkbox.

public void addItemListener(ItemListener l);

public void addItemListener(ItemListener l) method adds a listener for the ItemEvent objects this Checkbox generates.

Parameter
l – The listener to be added.

public void addNotify();

public void addNotify() method creates Checkbox peer.

public CheckboxGroup getCheckboxGroup();

public CheckboxGroup getCheckboxGroup() method returns the current CheckboxGroup associated with the Checkbox, if any.

This method returns the current CheckboxGroup associated with the Checkbox if any.

public String getLabel();

public String getLabel() method returns the text associated with the Checkbox.

This method returns the text associated with the Checkbox.

public Object[] getSelectedObjects();

public Object[] getSelectedObjects() method returns an array with length 1 if the Checkbox is checked containing the label of the Checkbox; otherwise returns null.

This method returns an array with length 1 if the Checkbox is checked containing the label of the Checkbox; otherwise returns null.

public boolean getState();

public boolean getState() method returns the current state of the Checkbox.

This method returns the current state of the Checkbox.

public void removeItemListener(ItemListener l);

public void removeItemListener(ItemListener l) method removes the specified ItemListener so it will not receive ItemEvent objects from this Checkbox.

Parameter
l – The listener to be removed.

public void setCheckboxGroup(CheckboxGroup group);

public void setCheckboxGroup(CheckboxGroup group) method associates the Checkbox with a different CheckboxGroup.

Parameter
group – New group in which to place the Checkbox.

public synchronized void setLabel(String label);

public synchronized void setLabel(String label) method changes the text associated with the Checkbox.

Parameter
label – New text to associate with Checkbox.

public void setState(boolean state);

public void setState(boolean state) method changes the state of the Checkbox.

Parameter
state – New state for the Checkbox.

protected String paramString();

protected String paramString() method is a helper method for toString() to generate a string of current settings.

This method returns the string with the current settings of Checkbox.

protected void processEvent(AWTEvent e);

protected void processEvent(AWTEvent e) method receives a Low-level AWTEvents for processing.

Parameter
e – The event to process.

protected void processItemEvent(ItemEvent e);

protected void processItemEvent(ItemEvent e) method receives Item events for processing. Normally, this method is called by processEvent().

Parameter
e – The item event to process.

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 the addItemListener() method. Post this we can receive notification in the item state changed event of 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
  • 29
  • 34
  • 56
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    166
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here