Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
A menu is a pull-down component of a menuBar. A menuBar can be attached to a component using the setMenuBar() method.
A menu can hold any combination of:
|
normal entry |
|
"-" indicated by a seperator |
|
sub-menu |
|
Item can be ticked on or off |
Menu example: (Run Applet)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MenuDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel implements ActionListener { private JLabel message = new JLabel("hello"); public View() { super(); final JMenuBar menuBar = new JMenuBar(); final JMenu mainMenu = new JMenu("Main Menu"); final JMenu subMenu = new JMenu("Sub Menu"); final JMenuItem one = new JMenuItem("one"); final JMenuItem two = new JMenuItem("two"); final JMenuItem three = new JMenuItem("three"); final JMenuItem subItem1 = new JMenuItem("subItem1"); final JMenuItem subItem2 = new JMenuItem("subItem2"); final JCheckBoxMenuItem checked = new JCheckBoxMenuItem("checked"); this.setLayout(new BorderLayout()); this.add("South", this.message); // add the items to the menu mainMenu.add(one); mainMenu.add(two); mainMenu.add(three); mainMenu.add(subMenu); mainMenu.add(checked); // add the items to the sub-menu subMenu.add(subItem1); subMenu.add(subItem2); // attach the menu bar to the applet setJMenuBar(menuBar); // add the menu to the menu bar menuBar.add(mainMenu); // add action listeners for the menu items one.addActionListener(this); two.addActionListener(this); three.addActionListener(this); checked.addActionListener(this); subItem1.addActionListener(this); subItem2.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { this.message.setText("Item '" + e.getActionCommand() + "' was selected."); } } }
Popup Menu Example: (Run Applet)
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.BevelBorder; public class PopupMenuDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel implements ActionListener, MouseListener { private final JLabel message = new JLabel(""); private final JPopupMenu mainMenu = new JPopupMenu(); public View() { super(); final JMenu subMenu = new JMenu("Sub Menu"); final JMenuItem one = new JMenuItem("one"); final JMenuItem two = new JMenuItem("two"); final JMenuItem three = new JMenuItem("three"); final JMenuItem subItem1 = new JMenuItem("subItem1"); final JMenuItem subItem2 = new JMenuItem("subItem2"); final JCheckBoxMenuItem checked = new JCheckBoxMenuItem("checked"); this.setLayout(new BorderLayout()); this.add("South", this.message); // add the items to the menu this.mainMenu.add(one); this.mainMenu.add(two); this.mainMenu.add(three); this.mainMenu.add(subMenu); this.mainMenu.add(checked); // add the items to the sub-menu subMenu.add(subItem1); subMenu.add(subItem2); // set the look-and-feel of the popup menu this.mainMenu.setBorder(new BevelBorder(BevelBorder.RAISED)); // add mouse listener to applet so that popup can be called this.addMouseListener(this); // add action listeners for the menu items one.addActionListener(this); two.addActionListener(this); three.addActionListener(this); checked.addActionListener(this); subMenu.addActionListener(this); subItem1.addActionListener(this); subItem2.addActionListener(this); } @Override public void mouseClicked(MouseEvent e) { this.mainMenu.show(this.getRootPane(), e.getX(), e.getY()); } // need to override all interface methods @Override public void mousePressed(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void actionPerformed(ActionEvent e) { this.message.setText("Item '" + e.getActionCommand() + "' was selected."); } } }
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.