Showing posts with label Checkbox control in java awt | how to create a button in java applet -Online Help. Show all posts
Showing posts with label Checkbox control in java awt | how to create a button in java applet -Online Help. Show all posts

Monday, 18 February 2019

Checkbox control in java awt | how to create a button in java applet -Online Help

CheckBox control in java AWT:

A checkbox control in Java awt is a user interface component. There may or may not be a check mark in the box. The state of the checkbox is changed by clicking the mouse on the text box or by using the setState() method.

Constructors in java awt checkbox:

The checkbox class defines the following constructors:

Checkbox()
It is used to create an empty checkbox().

Checkbox(String str)
It is used to create a checkbox with the specified string.

Checkbox(String str, Boolean state)
It is used to create a checkbox with the specified string and designated selection state.

Checkbox(String str, checkboxGroup grp, Boolean state)
It is used to create a checkbox with the specified string and the designated selection state. This causes it to interact with other members of the group so that only one was checked at a time, and the checkbox shape is changed to round.


Methods in java awt checkbox control:

The most commonly used checkbox() method are as follow:

void addItemListener(ItemListerner item list)
The addItemListener() method is used to configure an event handler for the checkbox.

Boolean getState()
The getState() method is used to get the current state of the checkbox.

void setState()
The setState() method is used to set the current state of the checkbox.

void setLabel(String str)
The setLabel() method is used to set the string of the checkbox.

void setFont(Font strFont)
The setFont() method is used to the font for the checkbox().

void setBackground(color bgcolor)
The setBackground() method is used to set the background color of the checkbox.

void setForeground(color strcolor)
The setForeground() method is used to set the color of the string for the checkbox.

Sample Program:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* <applet code="checkdemo" width=500 height=500>
</applet> */
public class checkdemo extends applet implements ItemListener
{
Checkbox c1,c2,c3,c4;
TextField t1;
public void init()
{
c1=new Checkbox("1");
add(c1);
c1.addItemListener(this);
c2=new Checkbox("2");
add(c2);
c2.addItemListener(this);
c3=new Checkbox("3");
add(c3);
c3.addItemListener(this);
c4=new Checkbox("4");
add(c4);
c4.addItemListener(this);
t1=new TextField(25);
add(t1);
}
public void itemStateChanged(ItemEvent e)
{
t1.setText("checkbox"+((checkbox)e.getItemSelectable()).getLabel()+"clicked");
}
}