Detecting a state change on my JCheckBox

Add an ItemListener and put your behavior in itemStateChanged.

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame {
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });
 
      final JCheckBox cb = new JCheckBox("Sample jcheckbox");
      cb.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            System.out.println("Checkbox state is now " + cb.isSelected());
         }
      });
 
      getContentPane().add(BorderLayout.NORTH, cb);
   } 
  
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(300, 100);
      main.setVisible(true);
   }
}