Checkbox control in java

Checkbox

 A check box is square shaped box which display an option to                the user.
 There is a label associated with each check box

Constructors

Checkbox( )
Checkbox(String str)
Checkbox(String str, boolean on) Allows to set the initial state of check box
Checkbox(String str, boolean on, CheckboxGroup cbGroup)

Checkbox(String str, CheckboxGroup cbGroup, boolean on)

Methods

boolean getState()
void setState(boolean on)
String getLabel()
void setLabel(String str)

Example

import java.awt.*;
import java.awt.event.*;
class test extends Frame implements ItemListener
{
String msg=” “;
Checkbox c1,c2,c3;
test()
{
setLayout(new FlowLayout());
c1 = new Checkbox(“bold”, true);
c2 = new Checkbox(“italic”);
c3 = new Checkbox(“underline”);
add(c1);
add(c2);
add(c3);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);

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

public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString(“Current state:”, 10, 100);
msg=”bold:”+c1.getState();
g.drawString(msg, 10, 120);
msg=”italic:” +c2.getState();
g.drawString(msg, 10, 140);
msg=”underline:”+c3.getState();
g.drawString(msg, 10, 160);
}
public static void main(String args[])
{
test t = new test();

t.setTitle(“My Check Box”);
t.setSize(400, 400);
t.setVisible(true);
}
}

Output:

Untitled

Leave a Reply

Your email address will not be published. Required fields are marked *