Skip to main content

CheckBoxGroup in Java

Grouping Checkbox in Java

CheckBox is used to select  of choices from the given items. The CheckBoxGroup enable to select only one choice from list of check box choices. In this example we create check box with color option ChkVio,ChkInd,ChkBlu,ChkGre,ChkYel,ChkOra,ChkRed. Checkboxgroup object is passed to every color checkbox and restrict the user to select only one color. 
ChkInd=new Checkbox("Indigo",ChkGpCol,false) in this checkbox name is Indigo and ChkGpCol is CheckboxGroup object.


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
//<applet code="chkcolor" width=500 height=250></applet>

public class chkcolor extends Applet implements ItemListener
{
Checkbox ChkVio,ChkInd,ChkBlu,ChkGre,ChkYel,ChkOra,ChkRed;
CheckboxGroup ChkGpCol;
public void init()
{
ChkGpCol=new CheckboxGroup();
ChkVio=new Checkbox("Violet",ChkGpCol,false);
ChkVio.addItemListener(this);
add(ChkVio);
ChkInd=new Checkbox("Indigo",ChkGpCol,false);
ChkInd.addItemListener(this);
add(ChkInd);
ChkBlu=new Checkbox("Blue",ChkGpCol,false);
ChkBlu.addItemListener(this);
add(ChkBlu);
ChkGre=new Checkbox("Green",ChkGpCol,false);
ChkGre.addItemListener(this);
add(ChkGre);
ChkYel=new Checkbox("Yellow",ChkGpCol,false);
ChkYel.addItemListener(this);
add(ChkYel);
ChkOra=new Checkbox("Orange",ChkGpCol,false);
ChkOra.addItemListener(this);
add(ChkOra);
ChkRed=new Checkbox("Red",ChkGpCol,false);
ChkRed.addItemListener(this);
add(ChkRed);
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==ChkVio)
setBackground(Color.black);
else if(ie.getSource()==ChkInd)
setBackground(Color.cyan);
else if(ie.getSource()==ChkBlu)
setBackground(Color.blue);
else if(ie.getSource()==ChkGre)
setBackground(Color.green);
else if(ie.getSource()==ChkYel)
setBackground(Color.yellow);
else if(ie.getSource()==ChkOra)
setBackground(Color.orange);
else
setBackground(Color.red);
}
}
CheckBoxGroup in Java




Thank You. Like Share and Subscribe 👍 folowlect.com

Comments