Sunday, August 25, 2013

JRadioButtonMenuItem

JRadioButtonMenuItem represents a radio button menu item. You can place radio button menu items in a group by using the ButtonGroup class. In the group only one menu item can be selected. You can add radio button menu items in the same way as you add the normal menu items to the menu object.

Constructors of the JRadioButtonMenuItem class:
-JRadioButtonMenuItem()
creates a radio button menu item without text or icon.
-JRadioButtonMenuItem(Action action)
creates a radio button menu item that its properties are taken from the Action object. The action of the menu item also can be specified with this Action object.
-JRadioButtonMenuItem(Icon icon)
creates a radio button menu item with the specified icon.
-JRadioButtonMenuItem(String text)
creates a radio button menu item with the specified text.
-JRadioButtonMenuItem(String text, boolean selected)
creates a radio button menu item with the specified text and selection state.
-JRadioButtonMenuItem(String text,Icon icon)
creates a radio button menu item with the specified text and icon.
-JRadioButtonMenuItem(String text,Icon icon,boolean selected)
creates a radio button menu item with the specified text, icon, and selection state.

In the example program below, we have two menus-File and Window. The File menu has two items-New and Close. These menu items are normal. The Window menu has two menu items too-Minimize and Maximize. These menu items are RadioButton menu items. They are grouped in a RadioButton so that only one item can be selected at a time. The AbstractAction class is extended to define the text property and action of each RadioButton menu item. The window can be minimized or maximized by selecting the Minimize or Maximize menu item.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JRadioButtonMenuItem;
class JRadioButtonMenuItemShow extends JFrame{
JMenu mfile;
JMenu mwin;
JMenuItem itemnew;
JMenuItem itemclose;
JRadioButtonMenuItem itemmax;
JRadioButtonMenuItem itemmin;

JRadioButtonMenuItemShow(String title){
setTitle(title);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new BorderLayout());
addWindowListener(new WinList());
setVisible(true);
JMenuBar mbar=new JMenuBar();
mfile=new JMenu("File");
mfile.setMnemonic(KeyEvent.VK_F);
itemnew=new JMenuItem(new AAction("New"));
itemclose=new JMenuItem(new AAction("Close"));
mfile.add(itemnew);
mfile.add(itemclose);
mwin=new JMenu(new AAction("Window"));
mwin.setMnemonic(KeyEvent.VK_W);
ButtonGroup bg=new ButtonGroup();
itemmax=new JRadioButtonMenuItem(new AAction("Maximize"));
itemmax.setSelected(true);
itemmin=new JRadioButtonMenuItem(new AAction("Minimize"));
bg.add(itemmax);
bg.add(itemmin);
mwin.add(itemmax);
mwin.add(itemmin);
mbar.add(mfile);
mbar.add(mwin);
   setJMenuBar(mbar);
   validate();
}


class AAction extends AbstractAction{

AAction(String text){
super(text);

}

AAction(String text,Icon c){
super(text, c);

}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Close"))
System.exit(0);
else if(e.getActionCommand().equals("Maximize"))
setExtendedState(JFrame.MAXIMIZED_BOTH);
else if(e.getActionCommand().equals("Minimize"))
setExtendedState(JFrame.ICONIFIED);
}
}
class WinList extends WindowAdapter{
public void windowIconified(WindowEvent e){
itemmin.setSelected(true);
}
public void windowDeiconified(WindowEvent e){
itemmax.setSelected(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}

}

public class JFrameSwing {
public static void main(String[] args){
new JRadioButtonMenuItemShow("JRadioButtonMenuItem");
}
}
JRadioButtonMenuItem Swing

No comments:

Post a Comment