This exhaustive example illustrates JRadioButton in Java for beginners. This tutorial covers all the core methods of javax.swing.JRadioButton class.
import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
class JRadioButtonExample extends JFrame
{
JPanel p;
JLabel label;
JRadioButton r1,r2,r3,r4,r5,r6,r7,r8;
ImageIcon i1,i2,i3,i4,i5,i6,i7;
ButtonGroup bg=new ButtonGroup();
public JRadioButtonExample()
{
// Create and show the GUI
createAndShowGUI();
}
private void createAndShowGUI()
{
// Set frame properties
setTitle("JRadioButton Demo");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create image icons
i1=new ImageIcon("radiobutton.png");
i2=new ImageIcon("radiobuttonselected.png");
i3=new ImageIcon("radiobuttonrollover.png");
i4=new ImageIcon("radiobuttondisabled.png");
i5=new ImageIcon("radiobuttonrolloverselected.png");
i6=new ImageIcon("radiobuttonpressed.png");
i7=new ImageIcon("radiobuttondisableselected.png");
// Create JPanel
// This panel holds all
// the JRadioButtons
p=new JPanel();
// Set the GridBagLayout
// for the panel
p.setLayout(new GridBagLayout());
// Create a JLabel
// This acts as a status label
// Initialize with text so that
// it gets displayed before packing
// other wise, the size would increase
// when the text is updated, and packing
// will be disturbed.
label=new JLabel(" ");
// First constructor
// Creates a JRadioButton
// with no text and icon
// and deselected
r1=new JRadioButton();
// Create an AbstractAction
// It is a class implementing the
// Action interface
// Make a beep sound
Action a=new DefaultEditorKit.BeepAction();
// Second constructor
// This takes javax.swing.Action
// object. This is fired whenever
// some action is performed on r2 (here)
r2=new JRadioButton(a);
// Third constructor
// This takes an ImageIcon
// This icon appears instead of the original
// UI representation of JRadioButton
r3=new JRadioButton(i1);
// Fourth constructor
// This takes an ImageIcon and
// a boolean value indicating
// selection/deselection
// The icon here is the default icon
// (normal icon) not the selected icon
// However, if there is no selected icon
// set separately, the same icon appears
r4=new JRadioButton(i1,true);
// Fifth constructor
// This takes String text which
// acts as label for the JRadioButton
r5=new JRadioButton("RadioButton 5");
// Sixth constructor
// This takes String text which acts
// as label and boolean indicating selection/
// deselection
// selection=true,deselection=false
r6=new JRadioButton("RadioButton 6",true);
// Seventh constructor
// This takes String label with an icon
// that acts as the default icon
r7=new JRadioButton("RadioButton 7",i1);
// Eighth constructor
// This takes String label, ImageIcon and
// selection flag
r8=new JRadioButton("RadioButton 8",i1,true);
// Set some text
r1.setText("Radio Button 1");
r3.setText("Radio Button 3");
r4.setText("Radio Button 4");
// Set some icon for r1
r1.setIcon(i1);
// Set selected icon
r1.setSelectedIcon(i2);
r4.setSelectedIcon(i2);
r7.setSelectedIcon(i2);
r8.setSelectedIcon(i2);
// Set roll over icon
r1.setRolloverIcon(i3);
// Set roll over selected icon
r1.setRolloverSelectedIcon(i5);
// Set pressed icon
r1.setPressedIcon(i6);
// Set disabled icon
// Will only be dispalyed
// if r3 is disabled
r3.setDisabledIcon(i4);
// Disable r3
r3.setEnabled(false);
// Make it selected
r3.setSelected(true);
// Set disabled selected icon
r3.setDisabledSelectedIcon(i7);
// Set focus painted false
// This removes border around
// the label of JRadioButton
// By default it is true
r1.setFocusPainted(false);
r4.setFocusPainted(false);
// Set border painted true
// Default it is false
r4.setBorderPainted(true);
// Set custom background
r4.setBackground(Color.DARK_GRAY);
// Set custom foreground
r4.setForeground(Color.WHITE);
// Set border
r4.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
// Now if you set roll over icon
// to r4 it wont get displayed
r4.setRolloverIcon(i3);
// Even the roll over selected icon
// will not work
r4.setRolloverSelectedIcon(i5);
// Set roll over enabled false
r4.setRolloverEnabled(false);
// Note:
// If the above two statements' positions
// are inter changed then there will be no
// effect of the method setRolloverEnabled(false)
// The methods setRolloverIcon() and setRolloverSelectedIcon()
// internally call setRolloverEnabled(true);
// Set hand cursor for JRadioButton
r1.setCursor(new Cursor(Cursor.HAND_CURSOR));
r3.setEnabled(true);
// r1 will come selected
// But the status label will not
// show 'RadioButton 1 is selected'
// since the most recent change when
// the frame is opened will be r8.setSelected(false);
r1.setSelected(true);
// Add JRadioButtons to the ButtonGroup
// The use of adding JRadioButtons to a
// ButtonGroup is that, only one radio button
// can be selected in a ButtonGroup of several
// JRadioButtons i.e. if one is selected all others
// in the same ButtonGroup remain deselected automatically.
bg.add(r1);
bg.add(r2);
// bg.add(r3);
bg.add(r4);
bg.add(r5);
bg.add(r6);
bg.add(r7);
bg.add(r8);
// All the radio buttons in the ButtonGroup
// become unselected after they were added to
// ButtonGroup 'if and only if' they were
// selected previously.
// If you remove bg.add(r8); then the status
// label will show 'Radio Button 6 is deselected'
// since the setSelected(false) will only be fired
// in the add() in ButtonGroup when the JRadioButton
// is previously selected. Here r7 is not previously
// selected. But r6 is.
// Print the no.of buttons in the ButtonGroup bg
System.out.println("No. of buttons in the ButtonGroup "+bg.getButtonCount());
// Select r3
// If r3 is not in the
// ButtonGroup, selecting it
// will not alter selection of
// any other JRadioButton
// in the group, since it has no
// relation with the ButtonGroup
// If r3 is in the ButtonGroup
// and selected, then r3 will be
// considered as the most recent selected
// item though it was disabled
r3.setSelected(true);
// Add all JRadioButtons
p.add(r1);
p.add(r2);
p.add(r3);
p.add(r4);
p.add(r5);
p.add(r6);
p.add(r7);
p.add(r8);
// Add the panel, label
add(p);
add(label,BorderLayout.SOUTH);
// Pack the JFrame
// Pack after adding everything
pack();
// Center the JFrame
setLocationRelativeTo(null);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
new JRadioButtonExample();
}
});
}
}