Advertisement



< Prev
Next >



Checkbox class





Checkbox class is used to create a checkbox control, which contains a box that can be checked or unchecked by clicking on it. Checkbox is a component which extends Component class and it can be added to the container like Frame or a component like Panel.




Constructors of Checkbox


Constructor Description
public Checkbox() Creates a checkbox with no text, this checkbox is unchecked by default..
public Checkbox(String text) Creates a checkbox with a text, this checkbox is unchecked by default..
public Checkbox(String text, boolean b) Creates a checkbox with a text, this checkbox is checked or unchecked depending on the boolean value.





Methods of Checkbox class


Methods Description
public void setName(String text) Sets a name on the Checkbox, this name will not be displayed.
public String getName() Gets a String message of Checkbox, this name will not be displayed.
public void setLabel() Sets a String text on button.
public String getLabel() Gets the String text of this button.
public void setState(boolean b) Sets a state of Checkbox.
public boolean getState() Gets the state of Checkbox.





An example variants of Checkbox


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;



public class CheckboxEx1
{
JFrame jf;
Checkbox chk1, chk2, chk3, chk4;

CheckboxEx1()
{
jf= new JFrame("Variants of Checkbox");

//Creating an no-message Checkbox
chk1 = new Checkbox();		

//Creating a pre-selected Checkbox with a message 
chk2 = new Checkbox("Yes",true);

//Creating an unselected Checkbox with a message	
chk3 = new Checkbox("No");	
		

jf.add(chk1);
jf.add(chk2);
jf.add(chk3);

jf.setLayout(new FlowLayout());
jf.setSize(300,120);
jf.setVisible(true);
}

public static void main(String... ar)
{
new CheckboxEx1();
}

}

When you run the code, you are presented a window shown below -:

Figure 1




Advertisement




Handling Checkbox events when a Checkbox is checked or unchecked.


In the upcoming code, we are going to handle events when a Checkbox is checked or unchecked by implementing ItemListener interface.
import java.awt.*;
import java.awt.event.*;


public class CheckboxEx2 implements ItemListener
{
Frame jf;
Checkbox chk1, chk2;
Label label1;


CheckboxEx2()
{
jf= new Frame("Checkbox");
chk1 = new Checkbox("Water");
chk2 = new Checkbox("Coffee");
label1 = new Label();

jf.add(chk1);
jf.add(chk2);

chk1.addItemListener(this);
chk2.addItemListener(this);

jf.setLayout(new FlowLayout());
jf.setSize(232,150);
jf.setVisible(true);
}

public void itemStateChanged(ItemEvent ie)
{
Checkbox ch =(Checkbox)ie.getItemSelectable();
if(ch.getState()==true)
{
label1.setText(ch.getLabel()+ " is checked");
jf.add(label1);
jf.setVisible(true);
}
else
{
label1.setText(ch.getLabel()+ " is unchecked");
jf.add(label1);
jf.setVisible(true);
}
}

public static void main(String... ar)
{
new CheckboxEx2();
}

}

When you run the code, you are presented a window shown in the Figure2 below -:

Figure 2


When you check a checkbox an event ItemEvent is fired and you are presented a message to display which checkbox is last checked/unchecked. For example, when you check the checkbox with label- happy, you are notified like -

Figure 3


When you uncheck this checkbox, an event ItemEvent is fired you are notified with a message that you've unchecked the checkbox(including its name).

Figure 4




Please share this article -





< Prev
Next >
< Button
List >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement