Showing posts with label group. Show all posts
Showing posts with label group. Show all posts

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

Put JRadioButton into group

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

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


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

import java.awt.GridLayout;

public class PutJRadioButtonIntoGroup
{
public static void main(String[]args)
{
//Create five radio button that will be put into a group
JRadioButton firstRadioButton=new JRadioButton("First radio button");
JRadioButton secondRadioButton=new JRadioButton("Second radio button");
JRadioButton thirdRadioButton=new JRadioButton("Third radio button");
JRadioButton fourthRadioButton=new JRadioButton("Fourth radio button");
JRadioButton fifthRadioButton=new JRadioButton("Fifth radio button");

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

//Add all radio button into created group
bg.add(firstRadioButton);
bg.add(secondRadioButton);
bg.add(thirdRadioButton);
bg.add(fourthRadioButton);
bg.add(fifthRadioButton);

//Create a window using JFrame with title ( Put JRadioButton into group )
JFrame frame=new JFrame("Put JRadioButton into group");

//Set JFrame layout to grid layout
frame.setLayout(new GridLayout(5,1));

//Add all created button into group
frame.add(firstRadioButton);
frame.add(secondRadioButton);
frame.add(thirdRadioButton);
frame.add(fourthRadioButton);
frame.add(fifthRadioButton);

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

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

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


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

RELAXING NATURE VIDEO