Add JRadioButton into JFrame

Source code below will show you, how to create radio button in java using JRadioButton. It also show you how to use ButtonGroup that enable only one selection can make at radio button in one time.

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


import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ButtonGroup;

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

public class AddJRadioButtonIntoJFrame
{
public static void main(String[]args)
{
//Create JRadioButton with text(Chicken)
JRadioButton radioButton1=new JRadioButton("Chicken");

//Create JRadioButton with text(Duck)
JRadioButton radioButton2=new JRadioButton("Duck");

//Create JRadioButton with text(Cow)
JRadioButton radioButton3=new JRadioButton("Cow");

//Create JRadioButton with text(Horse)
JRadioButton radioButton4=new JRadioButton("Horse");

//Create JRadioButton with text(Sheep)
JRadioButton radioButton5=new JRadioButton("Sheep");

//Create JRadioButton with text(Chicken)
JRadioButton radioButton6=new JRadioButton("Chicken");

//Create JRadioButton with text(Duck)
JRadioButton radioButton7=new JRadioButton("Duck");

//Create JRadioButton with text(Cow)
JRadioButton radioButton8=new JRadioButton("Cow");

//Create JRadioButton with text(Horse)
JRadioButton radioButton9=new JRadioButton("Horse");

//Create JRadioButton with text(Sheep)
JRadioButton radioButton10=new JRadioButton("Sheep");

//Create button group will make at one time
//only one JRadioButton can select in that group.
//Create first JRadioButton group
ButtonGroup groupFirst=new ButtonGroup();
groupFirst.add(radioButton1);
groupFirst.add(radioButton2);
groupFirst.add(radioButton3);
groupFirst.add(radioButton4);
groupFirst.add(radioButton5);

//Create second JRadioButton group
ButtonGroup groupSecond=new ButtonGroup();
groupSecond.add(radioButton6);
groupSecond.add(radioButton7);
groupSecond.add(radioButton8);
groupSecond.add(radioButton9);
groupSecond.add(radioButton10);

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

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

//Create a JPanel that we use to add JRadioButton 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 radioButton1 - radioButton5 into first JPanel
panel.add(radioButton1);
panel.add(radioButton2);
panel.add(radioButton3);
panel.add(radioButton4);
panel.add(radioButton5);

//Add radioButton6 - radioButton10 into second JPanel
panel2.add(radioButton6);
panel2.add(radioButton7);
panel2.add(radioButton8);
panel2.add(radioButton9);
panel2.add(radioButton10);

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

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

Add JToggleButton into JFrame

Source code below will show how to add JToggleButton into JFrame.

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


import javax.swing.JToggleButton;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class AddJToggleButtonIntoJFrame
{
public static void main(String[]args)
{
//Create a JToggleButton with text(Java)
JToggleButton toggleButton=new JToggleButton("Java");

//Create a JToggleButton with text(Java_2) and selection state
//If selection state true, JToggleButton will be pressed
JToggleButton toggleButton2=new JToggleButton("Java_2",true);

//Create a JFrame with title(Add toggle button)
JFrame frame=new JFrame("Add toggle button");

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

//Add first JToggleButton into JFrame
frame.add(toggleButton);

//Add second JToggleButton into JFrame
frame.add(toggleButton2);

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

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

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


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

Put JButton into JFrame

Source code below will show you, how to add a JButton into JFrame. In java, we will use JButton to create button.

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


import javax.swing.JButton;
import javax.swing.JFrame;

public class AddJButtonIntoJFrame
{
public static void main(String[]args)
{
//Create a JButton with text CLICK ME
JButton button=new JButton("CLICK ME");

//Create a JFrame that will be a window
//It's title is, Put JButton into JFrame
JFrame frame=new JFrame("Put JButton into JFrame");

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

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

//Set JFrame size with :
//Width : 400 pixels
//Height : 100 pixels
frame.setSize(400,100);

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


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

Put JLabel into JFrame

Source code below will show you, how to add a JLabel into JFrame.

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


import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.SwingConstants;

public class AddJLabelIntoJFrame
{
public static void main(String[]args)
{
//Create a JLabel with text HI that we want to add into JFrame
//SwingConstants.CENTER will make HI locate at the center of JLabel
JLabel label=new JLabel("HI",SwingConstants.CENTER);

//Create a JFrame that will be a window
//Window title is, Put JLabel into JFrame
JFrame frame=new JFrame("Put JLabel into JFrame");

//Add JLabel into JFrame
frame.add(label);

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

//Set size for JFrame in pixels
//Width=400 pixels
//Height=100 pixels
frame.setSize(400,100);

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


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

What is JFrame ???

JFrame is a part of Java API (Application Programming Interface). It located in javax.swing class hierarchy. This is why, when you want to use it, you need to import it like this :
import javax.swing.JFrame;
It is use to create window in java.

Create a JFrame

Source code below will show you how to create a java common window using JFrame class.

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


import javax.swing.JFrame;

public class CreateAJFrame
{
public static void main(String[]args)
{
//Create a window with title "My first window"
JFrame frame=new JFrame("My first window");

//Set close operation when user click window close button
//Window close button here is button at the top right of the window
//with 'x' sign
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set size for window.In this case we set 300 pixel width and 300 pixel height
frame.setSize(300,300);

//Make your window visible
frame.setVisible(true);

}
}


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

Close JWindow using mouse click on it

Source code below will show you how to create a JWindow that can terminate when we click on it.

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


import javax.swing.JWindow;
import javax.swing.JLabel;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.awt.FlowLayout;

public class ClosingJWindowUsingMouseClick
{
public static void main(String[]args)
{
//Create a JWindow
JWindow window=new JWindow();

//Create a JLabel that we want to add to JWindow
JLabel label=new JLabel("Click me to exit");

//Set layout for JWindow
window.setLayout(new FlowLayout());

//Add label to JWindow
window.add(label);

//Create Mouse Listener that listen when someone click on that JWindow
MouseListener ml=new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
//Exit application
System.exit(0);
}
};

//Add Mouse Listener to JWindow
window.addMouseListener(ml);

//Set JWindow size with width=400 pixels and height=400 pixels
window.setSize(400,400);

//Make JWindow visible
window.setVisible(true);
}
}


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

RELAXING NATURE VIDEO