Showing posts with label check box. Show all posts
Showing posts with label check box. Show all posts

Select all JCheckBox in one click

Watch video below :


*********************************************************************
COMPLETE SOURCE CODE FOR : JCheckBoxMultipleSelectInOneClick.java
*********************************************************************

import javax.swing.JCheckBox;
import javax.swing.JFrame;

import java.awt.GridLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class JCheckBoxMultipleSelectInOneClick extends JFrame implements ActionListener
{
 //Create JCheckBox object
 JCheckBox a=new JCheckBox("Select All");
 JCheckBox b=new JCheckBox("Car");
 JCheckBox c=new JCheckBox("Lorry");
 JCheckBox d=new JCheckBox("Motorcycle");
 
 //Constructor for JCheckBoxMultipleSelectInOneClick class
 public JCheckBoxMultipleSelectInOneClick()
 {
  //Set JFrame title
  super("Select All JCheckBox In One Click");
  
  //Set JFrame layout
  setLayout(new GridLayout(4,1));
  
  //Add action listener to JCheckBox a
  a.addActionListener(this);
  
  //Add JCheckBox into JFrame
  add(a);
  add(b);
  add(c);
  add(d);
  
  //Set JFrame's default close operation when click on close window button
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  //Set JFrame size
  setSize(400,400);
  
  //Make JFrame locate at the center of screen
  setLocationRelativeTo(null);
  
  //Make JFrame visible to see
  setVisible(true);
 }
 
 //Override actionPerformed method in ActionListener interface
 public void actionPerformed(ActionEvent e)
 {
  if(e.getSource()==a)
  {
   //Set all JCheckBox to select when JCheckBox a is select  
   if(a.isSelected()==true)
   {
    b.setSelected(true);
    c.setSelected(true);
    d.setSelected(true);
   }
   
   //Set all JCheckBox to unselect when JCheckBox a is select 
   else
   {
    b.setSelected(false);
    c.setSelected(false);
    d.setSelected(false);
   }
  }
 }

 //Main method where this program start
 public static void main(String[]args)
 {
  JCheckBoxMultipleSelectInOneClick myFirstObject = new JCheckBoxMultipleSelectInOneClick();
 }
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Create check box using JCheckBox

Source code below will show you, how to create a check box in java with text HI using JCheckBox.

***********************************************************************
COMPLETE SOURCE CODE FOR : CreateCheckBoxUsingJCheckBox.java
***********************************************************************


import javax.swing.JCheckBox;
import javax.swing.JFrame;

public class CreateCheckBoxUsingJCheckBox
{
public static void main(String[]args)
{
//Create check box with text HI
JCheckBox checkBox=new JCheckBox("HI");

//Create JFrame to place created JCheckBox
JFrame frame=new JFrame("Create check box using JCheckBox");

//add created check box into JFrame
frame.add(checkBox);

//set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//set JFrame size
frame.setSize(450,65);

//make your JFrame visible. So we can see it
frame.setVisible(true);
}
}


***********************************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************************

Create check box in java

In java, you can create check box using JCheckBox class.

RELAXING NATURE VIDEO