Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Add JCheckBox into JFrame (Java)

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