Monday, 8 April 2019

JCheckBox in java |Online Help

JCheckBox in java:

     The JCheckBox in java is a class which is used to do multiple selections from the list of options. The state of the JCheckBox is changed by clicking the mouse on the JCheckBox.The JCheckBox class defines the constructors.

JCheckBox(String str)

It is used to create a JCheckBox with the specified string.

JCheckBox(String str, Boolean state)

It is used to create a JCheckBox with the specified string and initial state.

JCheckBox(String str, Icon icon)

It is used to create a JCheckBox with the specified string and icon.

Methods for JCheckBox in java:

The most commonly used JCheckBox()methods are as follows:

void setSelected(Boolean state)

The setSelected()method is used to change the state of the JCheckBox.

String getText()

The getText() method is used to return the text in the field.

void addItemListener(ItemListener il)

This method is used to configure an event handler to a checkbox.

void isSelected()

The is Selected ()method is used to return the state of the JCheckBox.

Sample Program for JCheckbox in java:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*<applet code="Jcheck"width=500 hieght=500></applet>*/
public class Jcheck extends JApplet implements
{
JChecjBox jc1,jc2,jc3;
JTextField jtf;
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
jc1=new JCheckBox("ug");
jc2=new JCheckBox("pg");
jc3=new JCheckBox("phd");
jc1.addItemListener(this);
jc2.addItemListener(this);
jc3.addItemListener(this);
c.add(jc1);
c.add(jc2);
c.add(jc3);
jtf=new JTextField(25);
c.add(jtf);
}
public void itemStateChanged(ItemEvent e)
{
if(e.getItemSelectable()==jc1)
{
jtf.setText("undergraduate");
}
else
if(e.getItemSelectable()==jc2)
{
jtf.setText("post graduate");
}
else
if(e.getItemSelectable()==jc3)
{
jtf.setText("reserch");
}
}
}
}

No comments:

Post a Comment