An example of radio button menu items in action : Button « Swing JFC « Java

An example of radio button menu items in action

An example of radio button menu items in action
// An example of radio button menu items in action.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.border.BevelBorder;

public class RadioButtonMenuItemExample extends JPanel {

  public JTextPane pane;

  public JMenuBar menuBar;

  public JToolBar toolBar;

  public RadioButtonMenuItemExample() {
    menuBar = new JMenuBar();
    JMenu justifyMenu = new JMenu("Justify");
    ActionListener actionPrinter = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          pane.getStyledDocument().insertString(0,"Action [" + e.getActionCommand()+ "] performed!\n", null);
        } catch (Exception ex) {
    JRadioButtonMenuItem leftJustify = new JRadioButtonMenuItem("Left",new ImageIcon("1.gif"));
    leftJustify.setAccelerator(KeyStroke.getKeyStroke('L', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    JRadioButtonMenuItem rightJustify = new JRadioButtonMenuItem("Right",new ImageIcon("2.gif"));
    rightJustify.setAccelerator(KeyStroke.getKeyStroke('R', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    JRadioButtonMenuItem centerJustify = new JRadioButtonMenuItem("Center",new ImageIcon("3.gif"));
    centerJustify.setAccelerator(KeyStroke.getKeyStroke('M', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    JRadioButtonMenuItem fullJustify = new JRadioButtonMenuItem("Full",new ImageIcon("4.gif"));
    fullJustify.setAccelerator(KeyStroke.getKeyStroke('F', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));

    ButtonGroup group = new ButtonGroup();


    menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));

  public static void main(String s[]) {

    RadioButtonMenuItemExample example = new RadioButtonMenuItemExample();
    example.pane = new JTextPane();
    example.pane.setPreferredSize(new Dimension(250, 250));
    example.pane.setBorder(new BevelBorder(BevelBorder.LOWERED));

    JFrame frame = new JFrame("Menu Example");
    frame.getContentPane().add(example.pane, BorderLayout.CENTER);


Related examples in the same category

1.Creating a JButton Component from Action
2.Various Swing buttonsVarious Swing buttons
3.Responding to button pressesResponding to button presses
4.Putting buttons on an appletPutting buttons on an applet
5.Uses reflection to create groups of different types of AbstractButtonUses reflection to create groups of different types of AbstractButton
6.The Swing-ified button exampleThe Swing-ified button example
7.Create a JButton that does not show focusCreate a JButton that does not show focus
8.Demonstration of button events including Action, Item and Change event typesDemonstration of button events including Action, Item and Change event types
9.The event demonstration program for JToggleButtonThe event demonstration program for JToggleButton
10.JToggleButton DemoJToggleButton Demo
11.A Selected ButtonA Selected Button
12.Button demo: Mnemonic, alignment and action command Button demo: Mnemonic, alignment and action command
13.Button action to change the panel backgroundButton action to change the panel background
14.HTML buttonHTML button
15.Creating a Multiline Label for a JButton Component
16.Lines are left justified. This label text will center the lines
17.Label text italicizes the second line
18.Changing the Label of a JButton Component
19.Ploygon ButtonPloygon Button
20.Simple Button UISimple Button UI
21.Displaying a Button with an Icon LabelDisplaying a Button with an Icon Label
22.Displaying a Button with a BorderDisplaying a Button with a Border
23.Displaying a Button with Varying IconsDisplaying a Button with Varying Icons
24.Displaying a Button with Various Label AlignmentsDisplaying a Button with Various Label Alignments
25.Working with Tooltip TextWorking with Tooltip Text
26.Sharing Models between Buttons
27.Sharing an Action between JButton ComponentsSharing an Action between JButton Components
28.Display Message
29.Button Html DemoButton Html Demo
30.Swing Button DemoSwing Button Demo
31.LoadSave: Button actionLoadSave: Button action
32.Button with ICON and Multiline Button with ICON and Multiline
33.Action Button SampleAction Button Sample
34.Mnemonic SampleMnemonic Sample
35.Swing Default ButtonSwing Default Button
36.Button Action SampleButton Action Sample
37.Button icons, a default button, HTML in a button,and button mnemonics.
38.Adding a Rollover and Pressed Icon to a JButton Component
39.Adding a Disabled Icon to a JButton Component
40.Setting the Gap Size Between the Label and Icon in a JButton Component
41.Moving the Icon in a JButton Component
42.Adding an Icon to a JButton Component
43.Moving the Label/Icon Pair in a JButton Component
44.If the action does not have an icon or a different icon must be used, add or change the icon using setIcon():
45.Dynamically update the appearance of a component
46.Simple ToggleButton SampleSimple ToggleButton Sample