Advertisement
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 | 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. |
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);
}
}
Advertisement
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 -:
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 -:
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement