jcheckbox in java
JCheckBox is a swing component in Java that represents a checkbox with a label. It allows users to select one or more items from a list of available options.
Here is an example of how to create and use JCheckBox in Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CheckBoxExample extends JFrame {
JCheckBox checkBox1, checkBox2;
JLabel label;
CheckBoxExample() {
checkBox1 = new JCheckBox("Java");
checkBox1.setBounds(100, 100, 150, 50);
checkBox2 = new JCheckBox("Python");
checkBox2.setBounds(100, 150, 150, 50);
label = new JLabel();
label.setBounds(100, 200, 300, 50);
add(checkBox1);
add(checkBox2);
add(label);
setSize(400, 400);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
checkBox1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
label.setText("Java Checkbox: " + (e.getStateChange() == 1 ? "checked" : "unchecked"));
}
});
checkBox2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
label.setText("Python Checkbox: " + (e.getStateChange() == 1 ? "checked" : "unchecked"));
}
});
}
public static void main(String[] args) {
new CheckBoxExample();
}
}Sourcw:eww.theitroad.comThis program creates two JCheckBox components with labels "Java" and "Python" and adds them to the frame. It also adds a JLabel to display the state of the checkboxes.
When the user selects or deselects a checkbox, an ItemListener is invoked that sets the text of the label to the state of the checkbox.
