private void addMenuItem( JMenu menu, String label, int mnemonic, String accessibleDescription, String actionCallbackName) throws NoSuchMethodException { JMenuItem menuItem = new JMenuItem(label, mnemonic); menuItem .getAccessibleContext() .setAccessibleDescription((accessibleDescription != null) ? accessibleDescription : label); final Method callback = getClass().getMethod(actionCallbackName, new Class[] {ActionEvent.class}); menuItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { callback.invoke(WordListScreen.this, new Object[] {e}); } catch (InvocationTargetException ex) { handleException(ex.getTargetException()); } catch (IllegalAccessException ex) { handleException(ex); } } }); menu.add(menuItem); }
/** Creates a generic menu item */ protected JMenuItem createMenuItem( JMenu menu, String label, char mnemonic, String accessibleDescription, Action action) { JMenuItem mi = (JMenuItem) menu.add(new JMenuItem(label)); mi.setMnemonic(mnemonic); mi.getAccessibleContext().setAccessibleDescription(accessibleDescription); mi.addActionListener(action); if (action == null) { mi.setEnabled(false); } return mi; }
/** Creates a JRadioButtonMenuItem for the Look and Feel menu */ private JMenuItem createLafMenuItem( JMenu menu, String label, char mnemonic, String accessibleDescription, String laf) { JMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(label)); lafMenuGroup.add(mi); mi.setMnemonic(mnemonic); mi.getAccessibleContext().setAccessibleDescription(accessibleDescription); mi.addActionListener(new ChangeLookAndFeelAction(this, laf)); mi.setEnabled(isAvailableLookAndFeel(laf)); return mi; }
// Rebuild the contents of the menu based on current program state public void UpdateMenuBar() { JMenu menu; int i; menuBar.removeAll(); // Build the first menu. menu = new JMenu("File"); menu.setMnemonic(KeyEvent.VK_F); menuBar.add(menu); buttonOpenFile = new JMenuItem("Open File...", KeyEvent.VK_O); buttonOpenFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK)); buttonOpenFile.getAccessibleContext().setAccessibleDescription("Open a g-code file..."); buttonOpenFile.addActionListener(this); menu.add(buttonOpenFile); menu.addSeparator(); // list recent files if (recentFiles != null && recentFiles.length > 0) { // list files here for (i = 0; i < recentFiles.length; ++i) { if (recentFiles[i] == null || recentFiles[i].length() == 0) break; buttonRecent[i] = new JMenuItem((1 + i) + " " + recentFiles[i], KeyEvent.VK_1 + i); if (buttonRecent[i] != null) { buttonRecent[i].addActionListener(this); menu.add(buttonRecent[i]); } } if (i != 0) menu.addSeparator(); } buttonExit = new JMenuItem("Exit", KeyEvent.VK_Q); buttonExit.getAccessibleContext().setAccessibleDescription("Goodbye..."); buttonExit.addActionListener(this); menu.add(buttonExit); menuBar.add(menu); // settings menu menu = new JMenu("Settings"); menu.setMnemonic(KeyEvent.VK_T); menu.getAccessibleContext().setAccessibleDescription("Adjust the robot settings."); JMenu subMenu = new JMenu("Port"); subMenu.setMnemonic(KeyEvent.VK_P); subMenu.getAccessibleContext().setAccessibleDescription("What port to connect to?"); subMenu.setEnabled(!running); ButtonGroup group = new ButtonGroup(); ListSerialPorts(); buttonPorts = new JRadioButtonMenuItem[portsDetected.length]; for (i = 0; i < portsDetected.length; ++i) { buttonPorts[i] = new JRadioButtonMenuItem(portsDetected[i]); if (recentPort.equals(portsDetected[i]) && portOpened) { buttonPorts[i].setSelected(true); } buttonPorts[i].addActionListener(this); group.add(buttonPorts[i]); subMenu.add(buttonPorts[i]); } subMenu.addSeparator(); buttonRescan = new JMenuItem("Rescan", KeyEvent.VK_N); buttonRescan.getAccessibleContext().setAccessibleDescription("Rescan the available ports."); buttonRescan.addActionListener(this); subMenu.add(buttonRescan); menu.add(subMenu); buttonConfig = new JMenuItem("Configure limits", KeyEvent.VK_L); buttonConfig.getAccessibleContext().setAccessibleDescription("Adjust the robot & paper shape."); buttonConfig.addActionListener(this); buttonConfig.setEnabled(portConfirmed && !running); menu.add(buttonConfig); buttonJogMotors = new JMenuItem("Jog Motors", KeyEvent.VK_J); buttonJogMotors.addActionListener(this); buttonJogMotors.setEnabled(portConfirmed && !running); menu.add(buttonJogMotors); buttonDrive = new JMenuItem("Drive Manually", KeyEvent.VK_R); buttonDrive.getAccessibleContext().setAccessibleDescription("Etch-a-sketch style driving"); buttonDrive.addActionListener(this); buttonDrive.setEnabled(portConfirmed && !running); menu.add(buttonDrive); menuBar.add(menu); // Draw menu menu = new JMenu("Draw"); menu.setMnemonic(KeyEvent.VK_D); menu.getAccessibleContext().setAccessibleDescription("Start & Stop progress"); buttonStart = new JMenuItem("Start", KeyEvent.VK_S); buttonStart.getAccessibleContext().setAccessibleDescription("Start sending g-code"); buttonStart.addActionListener(this); buttonStart.setEnabled(portConfirmed && !running); menu.add(buttonStart); buttonPause = new JMenuItem("Pause", KeyEvent.VK_P); buttonPause.getAccessibleContext().setAccessibleDescription("Pause sending g-code"); buttonPause.addActionListener(this); buttonPause.setEnabled(portConfirmed && running); menu.add(buttonPause); buttonHalt = new JMenuItem("Halt", KeyEvent.VK_H); buttonHalt.getAccessibleContext().setAccessibleDescription("Halt sending g-code"); buttonHalt.addActionListener(this); buttonHalt.setEnabled(portConfirmed && running); menu.add(buttonHalt); menuBar.add(menu); // tools menu menu = new JMenu("Tools"); buttonZoomOut = new JMenuItem("Zoom -"); buttonZoomOut.addActionListener(this); buttonZoomOut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, ActionEvent.ALT_MASK)); menu.add(buttonZoomOut); buttonZoomIn = new JMenuItem("Zoom +", KeyEvent.VK_EQUALS); buttonZoomIn.addActionListener(this); buttonZoomIn.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, ActionEvent.ALT_MASK)); menu.add(buttonZoomIn); menuBar.add(menu); // Help menu menu = new JMenu("Help"); menu.setMnemonic(KeyEvent.VK_H); menu.getAccessibleContext().setAccessibleDescription("Get help"); buttonAbout = new JMenuItem("About", KeyEvent.VK_A); menu.getAccessibleContext().setAccessibleDescription("Find out about this program"); buttonAbout.addActionListener(this); menu.add(buttonAbout); buttonCheckForUpdate = new JMenuItem("Check for updates", KeyEvent.VK_U); menu.getAccessibleContext().setAccessibleDescription("Is there a newer version available?"); buttonCheckForUpdate.addActionListener(this); buttonCheckForUpdate.setEnabled(false); menu.add(buttonCheckForUpdate); menuBar.add(menu); // finish menuBar.updateUI(); }