Wednesday, July 24, 2013

Checkbox vs. CheckboxGroup

With Checkbox class you are able to create a list of items that their states can be turned on or off. You can turn on or turn off multiple items. However, if you want to allow a single item to be turned on or off, you must place the checkboxes in CheckboxGroup.

The Checkbox can generate item event. To receive the item event, the application must implement ItemListener interface and uses the addItemListener(ItemListener listener) method. The ItemListener interface has itemStateChanged(ItemEvent e) that needs to be overridden to perform action when the user selects or deselect the items.

Constructors of Checkbox class:
-Checkbox()
creates a check box without label.
-Checbox(String label)
creates a check box with label.
-Checkbox(String label, boolean state)
creates a check box with label and allowing state input to turn on or turn off the check box.
-Checkbox(String label, boolean state, CheckboxGroupd group)
creates a check box with label and allowing state input to turn on or turn off the check box and specifying check box group.

Useful methods of the Checkbox class:

-getLabel():String
reads the label from the check box.
-getState():boolean
reads the state of the check box.
-setLabel(String label):void
sets the label to the check box.
-setState(boolean state):void
sets the state to the check box.



Example:

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class InterfaceShow extends Frame{
private Label lbl;
private Checkbox course1;
private Checkbox course2;
private Checkbox course3;
private Checkbox course4;

InterfaceShow(String title){
setTitle(title);
setLayout(new BorderLayout());
setSize(new Dimension(500,300));

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

course1=new Checkbox("C++",true);
course1.addItemListener(new ItemList());
course2=new Checkbox("VB.NET",false);
course2.addItemListener(new ItemList());
course3=new Checkbox("Java",true);
course3.addItemListener(new ItemList());
course4=new Checkbox("Software Engineering",false);
course4.addItemListener(new ItemList());
Panel p=new Panel();
p.setLayout(new GridLayout(5,1));
p.add(new Label("Select your favorite courses:"));
p.add(course1);
p.add(course2);
p.add(course3);
p.add(course4);
add(p, BorderLayout.NORTH);
lbl=new Label();
lbl.setForeground(Color.BLUE);
add(lbl,BorderLayout.CENTER);
setVisible(true);
validate();
}

class ItemList implements ItemListener{
public void itemStateChanged(ItemEvent e){
if(e.getSource()==course1){
lbl.setText("You have selected "+course1.getLabel());

}
else if(e.getSource()==course2){
lbl.setText("You have selected "+course2.getLabel());

}
else if(e.getSource()==course3){
lbl.setText("You have selected "+course3.getLabel());

}
else {
lbl.setText("You have selected "+course4.getLabel());
}

}
}
}
public class AWTControls {
public static void main(String[] args){
new InterfaceShow("Using Checkbox and CheckboxGroup");

}
}


checkbox multiple selections


If you want to allow only a single item selection, replace part of the code:
course1=new Checkbox("C++",true);
course1.addItemListener(new ItemList());
course2=new Checkbox("VB.NET",false);
course2.addItemListener(new ItemList());
course3=new Checkbox("Java",true);
course3.addItemListener(new ItemList());
course4=new Checkbox("Software Engineering",false);
course4.addItemListener(new ItemList());

with the code:
CheckboxGroup cg=new CheckboxGroup();
course1=new Checkbox("C++",false,cg);
course1.addItemListener(new ItemList());
course2=new Checkbox("VB.NET",false,cg);
course2.addItemListener(new ItemList());
course3=new Checkbox("Java",true,cg);
course3.addItemListener(new ItemList());
course4=new Checkbox("Software Engineering",false,cg);
course4.addItemListener(new ItemList());

checkbox single selection

No comments:

Post a Comment