TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Swing

Swing JCheckBox Class

Overview

In this tutorial, we will learn how to use Swing JCheckBox Class to create checkboxes. A checkbox GUI component allows users to turn something on and off.

Swing JCheckBox Class

We can use the Swing JCheckBox class to create checkboxes.
The JCheckBox class inherits all functionality of JToogleButton class. The class implements the Accessible interface. We can create a checkbox with text or with an icon or both.

Java Program

package com.testingdocs.swing.components;

/**********************************************
 * FileName: CheckBoxDemo.java
 * Package : com.testingdocs.swing.components
 * 
 * Java Tutorials - www.TestingDocs.com
 **********************************************/

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class CheckBoxDemo {

 public static void main(String[] args) {
 //Create a frame instance
 JFrame frame = new JFrame("Swing CheckBox - www.TestingDocs.com");
 // Create a checkbox instance
 JCheckBox chkBox = new JCheckBox("Checkbox Example");
 
 chkBox.addItemListener(
 new ItemListener() {
 public void itemStateChanged(ItemEvent e) {
 if (e.getStateChange() == ItemEvent.SELECTED) {
 JOptionPane.showMessageDialog(frame,
 "CheckBox is checked");
 } 
 }
 });
 // Create Panel instance
 JPanel panel = new JPanel();
 panel.add(chkBox);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(400, 200);
 frame.getContentPane().add(panel, BorderLayout.SOUTH);
 frame.setVisible(true);
 }
}

Output

Run the Java application to view the output.

 

Related Posts

Java Swing /

Add GUI Components to a Swing Container

Java Swing /

Swing JPanel Container Class

Java Swing /

Java Swing CardLayout

Java Swing /

Java Swing GridLayout

Java Swing /

Java Swing FlowLayout

‹ Create a button with Swing JButton› Create a Swing Frame window

Recent Posts

  • Scaler Academy – An Online Learning Platform
  • Difference between PHP and JavaScript?
  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com