Advertisement
RupeshAcharya60

JCheckBox with Java Swing

Mar 24th, 2023
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | Source Code | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. class CheckBoxExample extends JFrame {
  6.     public CheckBoxExample() {
  7.         setTitle("Checkbox Example");
  8.         setSize(300, 200);
  9.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10.  
  11.         JPanel panel = new JPanel();
  12.         add(panel);
  13.  
  14.         JCheckBox checkBox = new JCheckBox("Check Me");
  15.         panel.add(checkBox);
  16.  
  17.         checkBox.addItemListener(new ItemListener() {
  18.             public void itemStateChanged(ItemEvent e) {
  19.                 if (e.getStateChange() == ItemEvent.SELECTED) {
  20.                     System.out.println("Checkbox selected");
  21.                 } else {
  22.                     System.out.println("Checkbox deselected");
  23.                 }
  24.             }
  25.         });
  26.     }
  27.  
  28.  
  29. }
  30.  
  31.  
  32. public class GUI{
  33.     public static void main(String[] args) {
  34.         CheckBoxExample example = new CheckBoxExample();
  35.         example.setVisible(true);
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement