JMenu In Java Swing Example

Chapter: Swing Last Updated: 20-07-2016 06:21:46 UTC

Program:

            /* ............... START ............... */
                
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

public class JavaJMenuDemo {

	JTextArea output;
	JScrollPane scrollPane;

	public JMenuBar createMenuBar() {
		JMenuBar menuBar;
		JMenu menu, submenu;
		JMenuItem menuItem;
		JRadioButtonMenuItem rbMenuItem;
		JCheckBoxMenuItem cbMenuItem;

		menuBar = new JMenuBar();

		menu = new JMenu("A Menu");
		menu.setMnemonic(KeyEvent.VK_A);
		menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
		menuBar.add(menu);

		menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
		menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
		menu.add(menuItem);

		ImageIcon icon = createImageIcon("images/middle.gif");
		menuItem = new JMenuItem("Both text and icon", icon);
		menuItem.setMnemonic(KeyEvent.VK_B);
		menu.add(menuItem);

		menuItem = new JMenuItem(icon);
		menuItem.setMnemonic(KeyEvent.VK_D);
		menu.add(menuItem);

		menu.addSeparator();
		ButtonGroup group = new ButtonGroup();

		rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
		rbMenuItem.setSelected(true);
		rbMenuItem.setMnemonic(KeyEvent.VK_R);
		group.add(rbMenuItem);
		menu.add(rbMenuItem);

		rbMenuItem = new JRadioButtonMenuItem("Another one");
		rbMenuItem.setMnemonic(KeyEvent.VK_O);
		group.add(rbMenuItem);
		menu.add(rbMenuItem);

		menu.addSeparator();
		cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
		cbMenuItem.setMnemonic(KeyEvent.VK_C);
		menu.add(cbMenuItem);

		cbMenuItem = new JCheckBoxMenuItem("Another one");
		cbMenuItem.setMnemonic(KeyEvent.VK_H);
		menu.add(cbMenuItem);

		menu.addSeparator();
		submenu = new JMenu("A submenu");
		submenu.setMnemonic(KeyEvent.VK_S);

		menuItem = new JMenuItem("An item in the submenu");
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
		submenu.add(menuItem);

		menuItem = new JMenuItem("Another item");
		submenu.add(menuItem);
		menu.add(submenu);

		menu = new JMenu("Another Menu");
		menu.setMnemonic(KeyEvent.VK_N);
		menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
		menuBar.add(menu);

		return menuBar;
	}

	public Container createContentPane() {

		JPanel contentPane = new JPanel(new BorderLayout());
		contentPane.setOpaque(true);

		output = new JTextArea(5, 30);
		output.setEditable(false);
		scrollPane = new JScrollPane(output);

		contentPane.add(scrollPane, BorderLayout.CENTER);

		return contentPane;
	}

	protected static ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = JavaJMenuDemo.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

	private static void createAndShowGUI() {

		JFrame frame = new JFrame("MenuLookDemo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JavaJMenuDemo demo = new JavaJMenuDemo();
		frame.setJMenuBar(demo.createMenuBar());
		frame.setContentPane(demo.createContentPane());

		frame.setSize(450, 260);
		frame.setVisible(true);
	}

	public static void main(String[] args) {

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}
}
                /* ............... END ............... */
        

Output


JMenu In Java Swing Example

Tags

JMenu, Swing, Java

Similar Programs Chapter Last Updated
Java JOptionPane ShowConfirmDialog Swing 21-06-2017
Java JOptionPane ShowInputDialog Swing 21-06-2017
Java JOptionPane Warning Message Swing 20-06-2017
Java JOptionPane ShowMessageDialog Swing 20-06-2017
Java JList Events With ListSelectionListener Swing 18-06-2017
Java System Color Chooser Swing 22-09-2018
JCheckBoxMenuItem With ItemListener Swing 16-06-2017
JCheckBoxMenuItem Events With ActionListener Swing 16-06-2017
Java JCheckBoxMenuItem SetMnemonic Swing 16-06-2017
JMenuItem Disable Menuitem Swing 12-06-2017
Java Drop Down Menus Example Swing 12-06-2017
JMenuItem Events With An ActionListener Swing 12-06-2017
Java JTable With Tooltip Swing 08-06-2017
Java JTable Manually Positioning Swing 08-06-2017
Java Resize SplitPane Swing 25-12-2016
Java SplitPane Example Swing 25-12-2016
Java JSeparator Example Swing 17-12-2016
Java JScrollPane Example Swing 17-12-2016
Java JTable Example Swing 17-12-2016
Java JToolBar Example Swing 17-12-2016
Java JEditorPane Example Swing 17-12-2016
Java JColorChooser Example Swing 17-12-2016
Java JToggleButton Example Swing 17-12-2016
Java JRadioButtonMenuItem Example Swing 17-12-2016
Java JCheckBoxMenuItem Example Swing 17-12-2016
Java JButton Listener Swing 16-12-2016
Java Message Dialog Box Swing 15-12-2016
Java Input Dialog Box Swing 15-12-2016
Java Confirm Dialog Box Swing 15-12-2016
Java Popup Menu Swing 14-10-2016

1 2 3 4