Showing posts with label jradiobutton. Show all posts
Showing posts with label jradiobutton. Show all posts

Get last selected JRadioButton

Complete source code below will show you, how to get last selected JRadioButton when we click on other JRadioButton. I hope you will understand what i want to tell you, or you can try to compile and execute source code below.

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


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class GetLastSelectedJRadioButton implements ItemListener
{
JRadioButton jrd1=new JRadioButton("a");
JRadioButton jrd2=new JRadioButton("b");
JRadioButton jrd3=new JRadioButton("c");
JRadioButton jrd4=new JRadioButton("d");

JRadioButton last=null;

ButtonGroup bg=new ButtonGroup();

JFrame myFrame=new JFrame("Get last selected JRadioButton");

public GetLastSelectedJRadioButton()
{
jrd1.addItemListener(this);
jrd2.addItemListener(this);
jrd3.addItemListener(this);
jrd4.addItemListener(this);

bg.add(jrd1);
bg.add(jrd2);
bg.add(jrd3);
bg.add(jrd4);

myFrame.getContentPane().setLayout(new GridLayout(4,1));

myFrame.getContentPane().add(jrd1);
myFrame.getContentPane().add(jrd2);
myFrame.getContentPane().add(jrd3);
myFrame.getContentPane().add(jrd4);

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(400,400);
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}

public void itemStateChanged(ItemEvent event)
{
JRadioButton temp=(JRadioButton)event.getItem();

if(event.getStateChange()==ItemEvent.DESELECTED)
{
last=temp;
}
else if(event.getStateChange()==ItemEvent.SELECTED)
{
if(last!=null)
{
JOptionPane.showMessageDialog(myFrame,"LAST : "+last.getText(),"Last",JOptionPane.INFORMATION_MESSAGE);
}
}
}

public static void main(String[]args)
{
GetLastSelectedJRadioButton glsjrb=new GetLastSelectedJRadioButton();
}
}


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

Get selected JRadioButton from ButtonGroup

Complete source code below will show you, how to get selected JRadioButton from ButtonGroup.

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


import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.AbstractButton;

import java.awt.BorderLayout;

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

import java.util.Enumeration;

public class GetSelectedJRadioButtonFromButtonGroup implements ActionListener
{
//Create two radio button that will be put into a group
//Set start radio button selection to female
JRadioButton firstRadioButton=new JRadioButton("Female",true);
JRadioButton secondRadioButton=new JRadioButton("Male");

//Create a button with text ( What i select )
JButton button=new JButton("What i select");

//Create a window using JFrame with title ( Get selected JRadioButton from ButtonGroup)
JFrame frame=new JFrame("Get selected JRadioButton from ButtonGroup");

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

public GetSelectedJRadioButtonFromButtonGroup()
{
//Add all radio button into created group
bg.add(firstRadioButton);
bg.add(secondRadioButton);

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

//Create a panel that will be put radio button into it
JPanel panel=new JPanel();

//Add all created radio button into panel
panel.add(firstRadioButton);
panel.add(secondRadioButton);

//Add action listener to created button
button.addActionListener(this);

//Add panel and button into JFrame
frame.add(panel,BorderLayout.CENTER);
frame.add(button,BorderLayout.SOUTH);

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

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

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

//Action for button
//Get selected JRadioButton from ButtonGroup
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
Enumeration<AbstractButton> allRadioButton=bg.getElements();

while(allRadioButton.hasMoreElements())
{
JRadioButton temp=(JRadioButton)allRadioButton.nextElement();
if(temp.isSelected())
{
JOptionPane.showMessageDialog(frame,"You select : "+temp.getText());
}
}
}
}

public static void main(String[]args)
{
GetSelectedJRadioButtonFromButtonGroup gsjrbfbg=new GetSelectedJRadioButtonFromButtonGroup();
}
}



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

Put JRadioButton into list

Complete source code below will show you, how to put JRadioButton into list. During my try using java i found no way to put JRadioButton into JList. So i try to create it on my own. I hope you enjoy what i share with you.

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


import javax.swing.*;

import java.awt.GridLayout;

import java.awt.Color;

public class AddRadioButtonIntoList
{
public static void main(String[]args)
{
//Create radio button using JRadioButton that will be put into list
JRadioButton radioButton1=new JRadioButton("Duck");
JRadioButton radioButton2=new JRadioButton("Sheep");
JRadioButton radioButton3=new JRadioButton("Cow");
JRadioButton radioButton4=new JRadioButton("Buffalo");
JRadioButton radioButton5=new JRadioButton("Horse");
JRadioButton radioButton6=new JRadioButton("Goat");

//Set all radio button background color to white
radioButton1.setBackground(Color.WHITE);
radioButton2.setBackground(Color.WHITE);
radioButton3.setBackground(Color.WHITE);
radioButton4.setBackground(Color.WHITE);
radioButton5.setBackground(Color.WHITE);
radioButton6.setBackground(Color.WHITE);

//Create button group for JRadioButton
ButtonGroup bg=new ButtonGroup();

//Add all created radio button into button group
bg.add(radioButton1);
bg.add(radioButton2);
bg.add(radioButton3);
bg.add(radioButton4);
bg.add(radioButton5);
bg.add(radioButton6);

//Create panel that will be use to put all radio button into it
JPanel panel=new JPanel(new GridLayout(6,1));

//Add all created radio button into panel
panel.add(radioButton1);
panel.add(radioButton2);
panel.add(radioButton3);
panel.add(radioButton4);
panel.add(radioButton5);
panel.add(radioButton6);

//Create scroll bar for created panel
//Vertical scrollbar always show
//Horizontal scrollbar never show
JScrollPane scrollBar=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

//Create window using JFrame with title ( Add JRadioButton into list )
JFrame frame=new JFrame("Add JRadioButton into list");

//Add created scroll bar into JFrame
frame.add(scrollBar);

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

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

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


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

Get selected JRadioButton

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

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


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

import java.awt.BorderLayout;

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

public class GetSelectedJRadioButton implements ActionListener
{
//Create two radio button that will be put into a group
//Set start radio button selection to female
JRadioButton firstRadioButton=new JRadioButton("Female",true);
JRadioButton secondRadioButton=new JRadioButton("Male");

//Create a button with text ( What i select )
JButton button=new JButton("What i select");

//Create a window using JFrame with title ( Get selected JRadioButton )
JFrame frame=new JFrame("Get selected JRadioButton");

public GetSelectedJRadioButton()
{
//Create a radio button group using ButtonGroup
ButtonGroup bg=new ButtonGroup();

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

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

//Create a panel that will be put radio button into it
JPanel panel=new JPanel();

//Add all created radio button into panel
panel.add(firstRadioButton);
panel.add(secondRadioButton);

//Add action listener to created button
button.addActionListener(this);

//Add panel and button into JFrame
frame.add(panel,BorderLayout.CENTER);
frame.add(button,BorderLayout.SOUTH);

//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);
}

//Action for button
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
if(firstRadioButton.isSelected())
{
JOptionPane.showMessageDialog(frame,"You select : "+firstRadioButton.getText());
}

else if(secondRadioButton.isSelected())
{
JOptionPane.showMessageDialog(frame,"You select : "+secondRadioButton.getText());
}
}
}

public static void main(String[]args)
{
GetSelectedJRadioButton gsjrb=new GetSelectedJRadioButton();
}
}


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

JRadioButton not select at start

Complete source code below will show you, how to make JRadioButton not select at start.

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


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

import java.awt.FlowLayout;

public class RadioButtonNotSelectAtStart
{
public static void main(String[]args)
{
//Create radio button with not select at start
//You can make this radio button select on start by change false to true
JRadioButton radioButton=new JRadioButton("I am a radio button",false);

//Create a window using JFrame with title ( JRadioButton not select on start )
JFrame frame=new JFrame("JRadioButton not select on start");

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

//add created radio button into JFrame
frame.add(radioButton);

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

//Set JFrame size
frame.setSize(200,65);

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


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

JRadioButton select at start

Complete source code below will show you, how to make JRadioButton select at start.

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


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

import java.awt.FlowLayout;

public class RadioButtonSelectAtStart
{
public static void main(String[]args)
{
//Create radio button with select at start
//You can make this radio button not select on start by change true to false
JRadioButton radioButton=new JRadioButton("I am a radio button",true);


//Create a window using JFrame with title ( JRadioButton select on start )
JFrame frame=new JFrame("JRadioButton select on start");

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

//add created radio button into JFrame
frame.add(radioButton);

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

//Set JFrame size
frame.setSize(200,65);

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


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

Set JRadioButton text bold and italic

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

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


import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;

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

public class SetJRadioButtonTextBoldAndItalic
{
public static void main(String[]args)
{
//Create radio button using JRadioButton with text ( I am a radio button )
JRadioButton radioButton=new JRadioButton("I am a radio button");

//Create font.
//Font Name : Default radio button font
//Font Style : Bold And Italic
//Font Size : Default radio button font size
Font newRadioButtonFont=new Font(radioButton.getFont().getName(),Font.ITALIC+Font.BOLD,radioButton.getFont().getSize());

//Set JRadioButton font using new created font
radioButton.setFont(newRadioButtonFont);

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

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

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

//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 JRadioButton text italic

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

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


import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;

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

public class SetJRadioButtonTextItalic
{
public static void main(String[]args)
{
//Create radio button using JRadioButton with text ( I am a radio button )
JRadioButton radioButton=new JRadioButton("I am a radio button");

//Create font.
//Font Name : Default radio button font
//Font Style : Italic
//Font Size : Default radio button font size
Font newRadioButtonFont=new Font(radioButton.getFont().getName(),Font.ITALIC,radioButton.getFont().getSize());

//Set JRadioButton font using new created font
radioButton.setFont(newRadioButtonFont);

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

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

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

//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 JRadioButton text bold

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

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


import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;

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

public class SetJRadioButtonTextBold
{
public static void main(String[]args)
{
//Create radio button using JRadioButton with text ( I am a radio button )
JRadioButton radioButton=new JRadioButton("I am a radio button");

//Create font.
//Font Name : Default radio button font
//Font Style : Bold
//Font Size : Default radio button font size
Font newRadioButtonFont=new Font(radioButton.getFont().getName(),Font.BOLD,radioButton.getFont().getSize());

//Set JRadioButton font using new created font
radioButton.setFont(newRadioButtonFont);

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

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

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

//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 JRadioButton text size

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

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


import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;

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

public class SetJRadioButtonTextSize
{
public static void main(String[]args)
{
//Create radio button using JRadioButton with text ( I am a radio button )
JRadioButton radioButton=new JRadioButton("I am a radio button");

//Create font.
//Font Name : Default radio button font
//Font Style : Default radio button font style
//Font Size : 22
Font newRadioButtonFont=new Font(radioButton.getFont().getName(),radioButton.getFont().getStyle(),22);

//Set JRadioButton font using new created font
radioButton.setFont(newRadioButtonFont);

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

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

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

//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 JRadioButton text font

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

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


import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;

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

public class SetJRadioButtonTextFont
{
public static void main(String[]args)
{
//Create radio button using JRadioButton with text ( I am a radio button )
JRadioButton radioButton=new JRadioButton("I am a radio button");

//Create font.
//Font Name : Tahoma
//Font Style : Default radio button font style
//Font Size : Default radio button font size
Font newRadioButtonFont=new Font("Tahoma",radioButton.getFont().getStyle(),radioButton.getFont().getSize());

//Set JRadioButton font using new created font
radioButton.setFont(newRadioButtonFont);

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

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

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

//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 JRadioButton text

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

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


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

import java.awt.BorderLayout;

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

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

//Create JRadioButton with text ( I am a radio button )
JRadioButton radioButton=new JRadioButton("I am a radio button");

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

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

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

//Add created radio button into JFrame
frame.add(radioButton,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 radio button
JOptionPane.showMessageDialog(frame,"JRadioButton text is : "+radioButton.getText());
}
}

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


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

Set JRadioButton text color

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

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


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

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

public class SetJRadioButtonTextColor
{
public static void main(String[]args)
{
//Create radio button with text HI using JRadioButton
JRadioButton radioButton=new JRadioButton("HI");

//Create JFrame with title ( Set JRadioButton text color )
JFrame frame=new JFrame("Set JRadioButton 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 JRadioButton text color to color that you choose
radioButton.setForeground(color);

//Add JRadioButton into JFrame
frame.add(radioButton);

//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 JRadioButton background color

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

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


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

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

public class SetJRadioButtonBackgroundColor
{
public static void main(String[]args)
{
//Create radio button with text HI using JRadioButton
JRadioButton radioButton=new JRadioButton("HI");

//Create JFrame with title ( Set JRadioButton background color )
JFrame frame=new JFrame("Set JRadioButton 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 JRadioButton background color to color that you choose
radioButton.setBackground(color);

//Add JRadioButton into JFrame
frame.add(radioButton);

//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 radio button using JRadioButton

Source code below will show you, how to create a radio button in java with text HI using JRadioButton.

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


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

public class CreateRadioButtonUsingJRadioButton
{
public static void main(String[]args)
{
//Create radio button with text HI
JRadioButton radioButton=new JRadioButton("HI");

//Create JFrame to place created JRadioButton
JFrame frame=new JFrame("Create radio button using JRadioButton");

//add created radio button into JFrame
frame.add(radioButton);

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

RELAXING NATURE VIDEO