Showing posts with label jcheckbox. Show all posts
Showing posts with label jcheckbox. 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
*********************************************************************

Get selected JCheckBox

Complete source code below will show you, how to get selected JCheckBox.

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


import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.GridLayout;

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

public class GetSelectedJCheckBox implements ActionListener
{
//Create String that will be use to hold selected JCheckBox text
String selectedCheckBox="";

//Create check box using JCheckBox
JCheckBox checkBox1=new JCheckBox("Duck");
JCheckBox checkBox2=new JCheckBox("Chicken");
JCheckBox checkBox3=new JCheckBox("Cow");
JCheckBox checkBox4=new JCheckBox("Sheep");

//Create button using JButton with text ( Get selected JCheckBox )
JButton button=new JButton("Get selected JCheckBox");

//method actionPerformed that we override because we implement ActionListener
public void actionPerformed(ActionEvent event)
{
//Action for button
if(event.getSource()==button)
{
//Check first JCheckBox is selected or not
if(checkBox1.isSelected())
{
selectedCheckBox=selectedCheckBox+checkBox1.getText()+"\n";
}

//Check second JCheckBox is selected or not
if(checkBox2.isSelected())
{
selectedCheckBox=selectedCheckBox+checkBox2.getText()+"\n";
}

//Check third JCheckBox is selected or not
if(checkBox3.isSelected())
{
selectedCheckBox=selectedCheckBox+checkBox3.getText()+"\n";
}

//Check fourth JCheckBox is selected or not
if(checkBox4.isSelected())
{
selectedCheckBox=selectedCheckBox+checkBox4.getText()+"\n";
}

//Show message that tell you, what check box that you selected
JOptionPane.showMessageDialog(null,"Selected check box is : \n"+selectedCheckBox);

//Make selectedCheckBox string like initial with empty string
selectedCheckBox=new String("");
}
}

//Constructor
public GetSelectedJCheckBox()
{
//Create a window using JFrame with title ( Get selected JCheckBox )
JFrame frame=new JFrame("Get selected JCheckBox");

//Create layout that will be use by JFrame
GridLayout gl=new GridLayout(5,1);

//Add ActionListener to button
button.addActionListener(this);

//Set JFrame layout
frame.setLayout(gl);

//Add all created check box into JFrame
frame.add(checkBox1);
frame.add(checkBox2);
frame.add(checkBox3);
frame.add(checkBox4);

//Add created button into JFrame
frame.add(button);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350,200);
frame.setVisible(true);
}

//Main method
public static void main(String[]args)
{
GetSelectedJCheckBox gsjcb=new GetSelectedJCheckBox();
}
}


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

Put JCheckBox into group

Complete source code below will show you, how to create group for JCheckBox.

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


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

import java.awt.GridLayout;

public class PutJCheckBoxIntoGroup
{
public static void main(String[]args)
{
//Create a window using JFrame with title ( Put JCheckBox into group )
JFrame frame=new JFrame("Put JCheckBox into group");

//Create check box using JCheckBox
JCheckBox checkBox1=new JCheckBox("Duck");
JCheckBox checkBox2=new JCheckBox("Chicken");
JCheckBox checkBox3=new JCheckBox("Cow");
JCheckBox checkBox4=new JCheckBox("Sheep");

//Create layout that will be use by JFrame
GridLayout gl=new GridLayout(4,1);

//Create button group using ButtonGroup
ButtonGroup bg=new ButtonGroup();

//Add all created check box into button group
bg.add(checkBox1);
bg.add(checkBox2);
bg.add(checkBox3);
bg.add(checkBox4);

//Set JFrame layout
frame.setLayout(gl);

//Add all created check box into JFrame
frame.add(checkBox1);
frame.add(checkBox2);
frame.add(checkBox3);
frame.add(checkBox4);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350,200);
frame.setVisible(true);
}
}


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

Set JCheckBox text bold and italic

Source code below will show you, how to set JCheckBox text to bold and italic.

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


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

import java.awt.BorderLayout;
import java.awt.Font;

public class SetJCheckBoxTextBoldAndItalic
{
public static void main(String[]args)
{
//Create check box using JCheckBox with text ( I am a check box )
JCheckBox checkBox=new JCheckBox("I am a check box");

//Create font.
//Font Name : Default check box font
//Font Style : Bold And Italic
//Font Size : Default check box font size
Font newCheckBoxFont=new Font(checkBox.getFont().getName(),Font.ITALIC+Font.BOLD,checkBox.getFont().getSize());

//Set JCheckBox font using new created font
checkBox.setFont(newCheckBoxFont);

//Create a window using JFrame with title ( Set JCheckBox text bold and italic )
JFrame frame=new JFrame("Set JCheckBox text bold and italic");

//Set JFrame layout to BorderLayout
frame.setLayout(new BorderLayout());

//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(400,200);

//Make JFrame visible
frame.setVisible(true);
}
}


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

Set JCheckBox text italic

Source code below will show you, how to set JCheckBox text to italic.

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


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

import java.awt.BorderLayout;
import java.awt.Font;

public class SetJCheckBoxTextItalic
{
public static void main(String[]args)
{
//Create check box using JCheckBox with text ( I am a check box )
JCheckBox checkBox=new JCheckBox("I am a check box");

//Create font.
//Font Name : Default check box font
//Font Style : Italic
//Font Size : Default check box font size
Font newCheckBoxFont=new Font(checkBox.getFont().getName(),Font.ITALIC,checkBox.getFont().getSize());

//Set JCheckBox font using new created font
checkBox.setFont(newCheckBoxFont);

//Create a window using JFrame with title ( Set JCheckBox text italic )
JFrame frame=new JFrame("Set JCheckBox text italic");

//Set JFrame layout to BorderLayout
frame.setLayout(new BorderLayout());

//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(400,200);

//Make JFrame visible
frame.setVisible(true);
}
}


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

Set JCheckBox text bold

Source code below will show you, how to set JCheckBox text to bold.

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


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

import java.awt.BorderLayout;
import java.awt.Font;

public class SetJCheckBoxTextBold
{
public static void main(String[]args)
{
//Create check box using JCheckBox with text ( I am a check box )
JCheckBox checkBox=new JCheckBox("I am a check box");

//Create font.
//Font Name : Default check box font
//Font Style : Bold
//Font Size : Default check box font size
Font newCheckBoxFont=new Font(checkBox.getFont().getName(),Font.BOLD,checkBox.getFont().getSize());

//Set JCheckBox font using new created font
checkBox.setFont(newCheckBoxFont);

//Create a window using JFrame with title ( Set JCheckBox text bold )
JFrame frame=new JFrame("Set JCheckBox text bold");

//Set JFrame layout to BorderLayout
frame.setLayout(new BorderLayout());

//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(400,200);

//Make JFrame visible
frame.setVisible(true);
}
}


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

Set JCheckBox text size

Source code below will show you, how to set JCheckBox text size.

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


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

import java.awt.BorderLayout;
import java.awt.Font;

public class SetJCheckBoxTextSize
{
public static void main(String[]args)
{
//Create check box using JCheckBox with text ( I am a check box )
JCheckBox checkBox=new JCheckBox("I am a check box");

//Create font.
//Font Name : Default check box font
//Font Style : Default check box font style
//Font Size : 22
Font newCheckBoxFont=new Font(checkBox.getFont().getName(),checkBox.getFont().getStyle(),22);

//Set JCheckBox font using new created font
checkBox.setFont(newCheckBoxFont);

//Create a window using JFrame with title ( Set JCheckBox text size )
JFrame frame=new JFrame("Set JCheckBox text size");

//Set JFrame layout to BorderLayout
frame.setLayout(new BorderLayout());

//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(400,200);

//Make JFrame visible
frame.setVisible(true);
}
}


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

Set JCheckBox text font

Source code below will show you, how to set JCheckBox text font.

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


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

import java.awt.BorderLayout;
import java.awt.Font;

public class SetJCheckBoxTextFont
{
public static void main(String[]args)
{
//Create check box using JCheckBox with text ( I am a check box )
JCheckBox checkBox=new JCheckBox("I am a check box");

//Create font.
//Font Name : Tahoma
//Font Style : Default check box font style
//Font Size : Default check box font size
Font newCheckBoxFont=new Font("Tahoma",checkBox.getFont().getStyle(),checkBox.getFont().getSize());

//Set JCheckBox font using new created font
checkBox.setFont(newCheckBoxFont);

//Create a window using JFrame with title ( Set JCheckBox text font )
JFrame frame=new JFrame("Set JCheckBox text font");

//Set JFrame layout to BorderLayout
frame.setLayout(new BorderLayout());

//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(400,200);

//Make JFrame visible
frame.setVisible(true);
}
}


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

Get JCheckBox text

Source code below will show you how to get text from JCheckBox.

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


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JCheckBox;

import java.awt.BorderLayout;

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

public class GetJCheckBoxText implements ActionListener
{
//Create button with text ( Click me to get JCheckBox text )
JButton button=new JButton("Click me to get JCheckBox text");

//Create JCheckBox with text ( I am a checkbox )
JCheckBox checkBox=new JCheckBox("I am a checkbox");

//Create a window with title ( Get JCheckBox text )
JFrame frame=new JFrame("Get JCheckBox text");

public GetJCheckBoxText()
{
//Add action listener to button
button.addActionListener(this);

//Set JFrame layout to BorderLayout
frame.setLayout(new BorderLayout());

//Add created check box into JFrame
frame.add(checkBox,BorderLayout.CENTER);

//Add created button into JFrame
frame.add(button,BorderLayout.SOUTH);

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

//Set JFrame size
frame.setSize(400,500);

//Make JFrame visible
frame.setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
//Method getText will get text from check box
JOptionPane.showMessageDialog(frame,"JCheckBox text is : "+checkBox.getText());
}
}

public static void main(String[]args)
{
GetJCheckBoxText gjbt=new GetJCheckBoxText();
}
}


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

Set JCheckBox text color

Source code below will show you, how to set JCheckBox text color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


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

import java.awt.Color;
import java.awt.FlowLayout;

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

//Create JFrame with title ( Set JCheckBox text color )
JFrame frame=new JFrame("Set JCheckBox text color");

//Set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JCheckBox text color to color that you choose
checkBox.setForeground(color);

//Add JCheckBox into JFrame
frame.add(checkBox);

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

//Set JFrame size
frame.setSize(500,300);

//Make JFrame visible
frame.setVisible(true);
}
}


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

Set JCheckBox background color

Source code below will show you, how to set JCheckBox background color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


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

import java.awt.Color;
import java.awt.FlowLayout;

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

//Create JFrame with title ( Set JCheckBox background color )
JFrame frame=new JFrame("Set JCheckBox background color");

//Set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JCheckBox background color to color that you choose
checkBox.setBackground(color);

//Add JCheckBox into JFrame
frame.add(checkBox);

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

//Set JFrame size
frame.setSize(500,300);

//Make JFrame visible
frame.setVisible(true);
}
}


*************************************************************
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
***********************************************************************

Add JCheckBox into JFrame

Source code below will show you how to add JCheckBox into JPanel. After that we will add the JPanel into JFrame.

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


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

import java.awt.FlowLayout;
import java.awt.GridLayout;

public class AddJCheckBoxIntoJFrame
{
public static void main(String[]args)
{
//Create JCheckBox with text(Chicken)
JCheckBox checkBox1=new JCheckBox("Chicken");

//Create JCheckBox with text(Duck)
JCheckBox checkBox2=new JCheckBox("Duck");

//Create JCheckBox with text(Cow)
JCheckBox checkBox3=new JCheckBox("Cow");

//Create JCheckBox with text(Horse)
JCheckBox checkBox4=new JCheckBox("Horse");

//Create JCheckBox with text(Sheep)
JCheckBox checkBox5=new JCheckBox("Sheep");

//Create JCheckBox with text(Chicken)
JCheckBox checkBox6=new JCheckBox("Chicken");

//Create JCheckBox with text(Duck)
JCheckBox checkBox7=new JCheckBox("Duck");

//Create JCheckBox with text(Cow)
JCheckBox checkBox8=new JCheckBox("Cow");

//Create JCheckBox with text(Horse)
JCheckBox checkBox9=new JCheckBox("Horse");

//Create JCheckBox with text(Sheep)
JCheckBox checkBox10=new JCheckBox("Sheep");

//Create a JFrame with title ( Add JCheckBox into JFrame )
JFrame frame=new JFrame("Add JCheckBox into JFrame");

//Create a JPanel that we use to add JCheckBox into it
JPanel panel=new JPanel();

//Create a JPanel that we use to add JCheckBox into it
JPanel panel2=new JPanel();

//Set layout for JFrame
frame.setLayout(new FlowLayout());

//Set layout for first JPanel
panel.setLayout(new FlowLayout());

//Set layout for second JPanel
panel2.setLayout(new GridLayout(5,1));

//Add checkBox1 - checkBox5 into first JPanel
panel.add(checkBox1);
panel.add(checkBox2);
panel.add(checkBox3);
panel.add(checkBox4);
panel.add(checkBox5);

//Add checkBox6 - checkBox10 into second JPanel
panel2.add(checkBox6);
panel2.add(checkBox7);
panel2.add(checkBox8);
panel2.add(checkBox9);
panel2.add(checkBox10);

//Add first JPanel into JFrame
frame.add(panel);

//Add second JPanel into JFrame
frame.add(panel2);

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

//Set size for JFrame
frame.setSize(400,400);

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


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

RELAXING NATURE VIDEO