Advertisement



< Prev
Next >



JCheckBox class




JCheckBox class is used to create a checkbox control, which contains a box that can be checked or unchecked by clicking on it. JCheckBox is a component which extends JComponent class and it can be added to the container like JFrame or a component like JPanel.




Constructors of JCheckBox


Constructor Description
public JCheckBox() Creates a checkbox with no text, this checkbox is unchecked by default.
public JCheckBox(String text) Creates a checkbox with a text, this checkbox is unchecked by default..
public JCheckBox(String text, boolean b) Creates a checkbox with a text, this checkbox is checked or unchecked depending on the boolean value.
public JCheckBox(String text, Icon image, boolean b) Creates a checkbox with a text and an icon, this checkbox is checked or unchecked depending on the boolean value.





Methods of JCheckBox class


Methods Description
public void setName(String text) Sets a name on the JCheckBox, this name will not be displayed.
public String getName() Gets a String message of JCheckBox, this name will not be displayed.
public void setIcon(Icon icon) Sets an icon or image over the JCheckBox.
public Icon getIcon() Gets the icon or image of the JCheckBox.





An example variants of JCheckbox


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



public class CheckBox2
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class A


class A //implements ActionListener
{
JFrame jf;
JCheckBox chk1, chk2, chk3, chk4;

A()
{
jf= new JFrame("Variants of JCheckbox");

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

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

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

//Creating a JCheckBox with an icon(box of this JCheckBox will be covered with icon, hence, not visible).
chk4 = new JCheckBox("Maybe", new ImageIcon("Smiley3.jpg"), true); 		

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

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

}

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

Figure 1

Remember the last JCheckbox(ch4) will not be visible because its icon has covered it, but it is still a checkbox, just covered by its icon.


Advertisement




Handling JCheckBox events when a JCheckBox is checked or unchecked.


In the upcoming code, we are going to handle events when a JCheckBox is checked or unchecked. In order to do this, we must implement ActionListener interface and provide implementation of its method - actionPerformed(). In the next code, we have also combined a JCheckBox with a JLabel to add an icon/image next to a checkbox.

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


public class CheckBox3
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class A

class A implements ActionListener
{
JFrame jf;
JCheckBox chk1, chk2;
JLabel label1;

A()
{
jf= new JFrame("JCheckbox");
chk1 = new JCheckBox("Happy");
chk2 = new JCheckBox("Sad");
label1 = new JLabel();

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

chk1.addActionListener(this);
chk2.addActionListener(this);

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

public void actionPerformed(ActionEvent ae)
{
JCheckBox ab = (JCheckBox)ae.getSource();

if(ab.isSelected()==true)
{
label1.setText(ae.getActionCommand()+ " is checked");
jf.add(label1);
jf.setVisible(true);
}
else
{
label1.setText(ae.getActionCommand()+ " is unchecked");
jf.add(label1);
jf.setVisible(true);
}
}


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

Figure 2


When you check a checkbox an ActionEvent 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, you are notified with a message that you've unchecked the checkbox(including its its name).


Figure 4





JCheckBox with images


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


public class CheckBox4
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class A

class A implements ActionListener
{
JFrame jf;
JCheckBox chk1, chk2, chk3;
JLabel label1, label2, label3, label4;


A()
{
jf= new JFrame("JCheckbox with icons");

label1 = new JLabel(new ImageIcon("Blue.jpg"));
chk1 = new JCheckBox("");
chk1.setName("Blue");

label2 = new JLabel(new ImageIcon("Black.jpg"));
chk2 = new JCheckBox("");
chk2.setName("Black");

label3 = new JLabel(new ImageIcon("Red.jpg"));
chk3 = new JCheckBox("");
chk3.setName("Red");

label4= new JLabel();

jf.add(chk1);
jf.add(label1);
jf.add(chk2);
jf.add(label2);
jf.add(chk3);
jf.add(label3);

chk1.addActionListener(this);
chk2.addActionListener(this);
chk3.addActionListener(this);

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

public void actionPerformed(ActionEvent ae)
{
JCheckBox ab = (JCheckBox)ae.getSource();

if(ab.isSelected()==true)
{
label4.setText(ab.getName()+ " is checked");
jf.add(label4);
jf.setVisible(true);
}
else
{
label4.setText(ab.getName()+ " is unchecked");
jf.add(label4);
jf.setVisible(true);
}
}

}

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

Figure 5


When you check a checkbox an ActionEvent is fired and you are presented a message to display which color of checkbox is last checked/unchecked. For example, when you check the checkbox with red image, you are notified like -


Figure 6



when you uncheck this checkbox, you are notified with a message that you've unchecked the blue checkbox.

Figure 7




Please share this article -





< Prev
Next >
< JButton
JComboBox >



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