Memra

Menus, dialogs & shortcuts

Build a JMenuBar with mnemonics and accelerators, pop a JFileChooser to load an events file, and confirm Exit with JOptionPane — the Greenhouse panel’s File menu spec.

Menus are containers too

A menu bar is built bottom-up from three types: JMenuItem (a clickable command) goes into a JMenu (a drop-down), which goes into a JMenuBar, which you attach to the frame with frame.setJMenuBar(...) — *not* add(...), because the menu bar lives in the frame’s title-area slot, not the content pane.

Each JMenuItem fires an ActionEvent just like a button, so you reuse the ActionListener model from Lesson 11.2.

Mnemonics vs accelerators

Two different keyboard features, easy to confuse:

- A mnemonic is the underlined letter that opens or selects an item *while the menu has focus* (e.g. Alt+F to open the File menu). Set it with setMnemonic('F') or KeyEvent.VK_F. - An accelerator is a global shortcut that fires the item from *anywhere*, no menu navigation needed (e.g. Ctrl+O to Open). Set it with setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK)).

Dialogs

- JFileChooser — the file picker. showOpenDialog(parent) returns JFileChooser.APPROVE_OPTION if the user picked a file; then getSelectedFile() returns the File. This is how the panel loads an examples*.txt events file at runtime instead of hard-coding the name. - JOptionPane — quick standard dialogs. showConfirmDialog(...) returns JOptionPane.YES_OPTION / NO_OPTION; use it to confirm a destructive action like Exit.

Worked example — the File menu (New / Open / Restore / Exit)

JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);          // Alt+F opens the menu

JMenuItem open = new JMenuItem("Open");
open.setAccelerator(KeyStroke.getKeyStroke(
        KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK)); // Ctrl+O anywhere
open.addActionListener(e -> {
    JFileChooser chooser = new JFileChooser();
    if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
        File f = chooser.getSelectedFile();
        log.append("Loaded: " + f.getName() + "\n");
    }
});
file.add(open);

JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(e -> {
    int choice = JOptionPane.showConfirmDialog(
            frame, "Quit the control panel?", "Confirm Exit",
            JOptionPane.YES_NO_OPTION);
    if (choice == JOptionPane.YES_OPTION) System.exit(0);
});
file.add(exit);

JMenuBar bar = new JMenuBar();
bar.add(file);
frame.setJMenuBar(bar);

Trace the *Open* flow: Ctrl+O (or File ▸ Open) fires the listener; showOpenDialog blocks until the user chooses; if they hit OK the return value equals APPROVE_OPTION and getSelectedFile() yields the chosen File, whose name is logged. If they cancel, the if is false and nothing happens — always guard on APPROVE_OPTION before reading the file. *Exit* asks for confirmation first and only calls System.exit(0) on YES, so an accidental click is recoverable.

NORMAL ~/memra/learn/comp-308/menus-dialogs-shortcuts utf-8 LF