Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Krantik Chavan

The following is an example to get or set the selection state of JCheckBox:

Example

import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class SwingDemo extends JFrame {
   public SwingDemo() {
      setSize(500, 500);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(new FlowLayout(FlowLayout.CENTER));
      JCheckBox checkBox = new JCheckBox("Demo");
      checkBox.setSelected(true);
      boolean sel = checkBox.isSelected();
      if (sel)
         System.out.println("Check box selected!");
      getContentPane().add(checkBox);
   }
   public static void main(String[] args) {
      new SwingDemo().setVisible(true);
   }
}

Output

Since the checkbox is selected by default, the following output would be visible in EclipseIDE:

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.