| | |
    Java Beginner Home
    Table of Contents
    Swing Tutorial
     JFrame
     JInternalFrame
     JWindow
     JOptionPane
     JLabel
     JTextField
     JPasswordField
     JTextArea
     JButton
     JRadioButton
     JCheckBox
     JComboBox
     Jlist
     JTabbedPane
     JMenuBar
     Scrollable JPopupMenu
     JToolBar
     BorderLayout
     FlowLayout
     GridLayout
     GridBagLayout
     Java Look and Feel
     Swing Calculator
     Swing Address book
     Shuffle Game
    Download Java Software
    Advertise
    Feedback
    Java books
    Eclipse Plugin Site

Java JRadioButton class example



JRadioButton

Java Swing Tutorial Explaining the JRadioButton Component. JRadioButton is similar to JCheckbox, except for the default icon for each class. A set of radio buttons can be associated as a group in which only one button at a time can be selected.

JRadioButton Source Code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JRadioButtonDemo extends JPanel {

	static JFrame frame;
	JLabel jlbPicture;
	RadioListener myListener = null;
	public JRadioButtonDemo() {
		// Create the radio buttons and assign Keyboard shortcuts using
		// Mnemonics
		JRadioButton jrbNumbers = new JRadioButton("Numbers");
		jrbNumbers.setMnemonic(KeyEvent.VK_N);
		jrbNumbers.setActionCommand("numbers");
		jrbNumbers.setSelected(true);
		JRadioButton jrbAlphabets = new JRadioButton("Alphabets");
		jrbAlphabets.setMnemonic(KeyEvent.VK_A);
		jrbAlphabets.setActionCommand("alphabets");
		JRadioButton jrbSymbols = new JRadioButton("Symbols");
		jrbSymbols.setMnemonic(KeyEvent.VK_S);
		jrbSymbols.setActionCommand("symbols");
		// Group the radio buttons.
		ButtonGroup group = new ButtonGroup();
		group.add(jrbNumbers);
		group.add(jrbAlphabets);
		group.add(jrbSymbols);
		// Register an action listener for the radio buttons.
		myListener = new RadioListener();
		jrbNumbers.addActionListener(myListener);
jrbAlphabets.addActionListener(myListener); jrbSymbols.addActionListener(myListener); // Set up the picture label jlbPicture = new JLabel(new ImageIcon("" + "numbers" + ".jpg")); // Set the Default Image jlbPicture.setPreferredSize(new Dimension(177, 122)); // Put the radio buttons in a column in a panel JPanel jplRadio = new JPanel(); jplRadio.setLayout(new GridLayout(0, 1)); jplRadio.add(jrbNumbers); jplRadio.add(jrbAlphabets); jplRadio.add(jrbSymbols); setLayout(new BorderLayout()); add(jplRadio, BorderLayout.WEST); add(jlbPicture, BorderLayout.CENTER); setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); } /** Listens to the radio buttons. */ class RadioListener implements ActionListener { public void actionPerformed(ActionEvent e) { jlbPicture.setIcon(new ImageIcon("" + e.getActionCommand() + ".jpg")); } } public static void main(String s[]) { frame = new JFrame("JRadioButton Usage Demo"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.getContentPane().add(new JRadioButtonDemo(), BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } }

Output

Download jRadioButton Source Code

Java JRadioButton Hierarchy

javax.swing
Class JRadioButton
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.AbstractButton
javax.swing.JToggleButton
javax.swing.JRadioButton
All Implemented Interfaces:
Accessible, ImageObserver, ItemSelectable, MenuContainer, Serializable, SwingConstants

JRadioButton Constructor

JRadioButton()
Creates an initially unselected radio button with no set text.

JRadioButton(Action a)
Creates a radiobutton where properties are taken from the Action supplied.

JRadioButton(Icon icon)
Creates an initially unselected radio button with the specified image but no text.

JRadioButton(Icon icon, boolean selected)
Creates a radio button with the specified image and selection state, but no text.

JRadioButton(String text)
Creates an unselected radio button with the specified text.

JRadioButton(String text, boolean selected)
Creates a radio button with the specified text and selection state.

JRadioButton(String text, Icon icon)
Creates a radio button that has the specified text and image, and that is initially unselected.

JRadioButton(String text, Icon icon, boolean selected)
Creates a radio button that has the specified text, image, and selection state.

 
 
    Java is a trademark of Sun Microsystems, Inc.
| | |
© Copyright 2007-08 javabeginner.com