Example of Java AWT Checkbox
Example 1:
Java
// Java Program to demonstrate // Creating a Java AWT Checkbox import java.awt.*; public class one { //Constructor to initialize public one() { // Creating a Frame Frame frame = new Frame( "Simple Checkbox Example" ); // Creating a Checkbox with the label "Agree to terms and conditions" Checkbox checkbox = new Checkbox( "Agree to w3wiki terms and conditions" ); // Setting Checkbox position checkbox.setBounds( 50 , 50 , 250 , 30 ); // Adding Checkbox to the frame frame.add(checkbox); // Setting Frame size frame.setSize( 400 , 200 ); // Setting Layout to null frame.setLayout( null ); // Making the frame visible frame.setVisible( true ); } public static void main(String[] args) { new one(); } } |
Output:
Example 2:
Java
// Java Program to demonstrate Checkbox example using an // ItemListener import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class one { // Constructor to initialize public one() { // Creating a Frame Frame frame = new Frame( "Checkbox with ItemListener Example" ); // Creating a Checkbox Checkbox checkbox = new Checkbox( "Enable Feature" ); // Setting Checkbox position checkbox.setBounds( 50 , 50 , 150 , 30 ); // Adding ItemListener to Checkbox checkbox.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { // Handling Checkbox state change event if (checkbox.getState()) { System.out.println( "Feature is enabled" ); } else { System.out.println( "Feature is disabled" ); } } }); // Adding Checkbox to the frame frame.add(checkbox); // Setting Frame size frame.setSize( 400 , 200 ); // Setting Layout to null frame.setLayout( null ); // Making the frame visible frame.setVisible( true ); } public static void main(String[] args) { new one(); } } |
Output
When check box gets unchececked
Java AWT Checkbox
Java AWT (Abstract Window Toolkit) provides a really various set of tools for edifice in writing user interfaces (GUIs), and among these tools is the Checkbox class. Checkboxes are necessary components for user interactions, allowing users to work binary choices easily. In this clause, we’ll search the Checkbox class, its constructors, and methods, and supply examples of creating checkboxes with really different logic.