public MainMenuBar(JFrame pf, final AddressbookController controller) { this.controller = controller; JMenu fileMenu = new JMenu("File"); JMenu editMenu = new JMenu("Items"); parent = pf; // Assemble the File menus with mnemonics ActionListener printListener = new ActionListener() { public void actionPerformed(ActionEvent event) { String cmd = event.getActionCommand(); System.out.println("Menu item [" + cmd + "] was pressed."); if (cmd.equals("Save")) controller.saveModelToFile(); else if (cmd.equals("Exit")) System.exit(0); else if (cmd.equals("Import")) new CSVFileHandler(parent, cmd, controller); else if (cmd.equals("Export")) new CSVFileHandler(parent, cmd, controller); } }; for (int i = 0; i < fileItems.length; i++) { JMenuItem item = new JMenuItem(fileItems[i], fileShortcuts[i]); item.addActionListener(printListener); fileMenu.add(item); } // for for (int i = 0; i < editItems.length; i++) { JMenuItem item = new JMenuItem(editItems[i], editShortcuts[i]); item.addActionListener(printListener); editMenu.add(item); } // for // Insert a separator in the File Menu in Position 1 after "Save" fileMenu.insertSeparator(1); // Finally, add all the menus to the menu bar add(fileMenu); add(editMenu); } // MainMenuBar
public void insertSeparatorAtIndex(int index) { items.add(index, new XJMenuItemSeparator()); jMenu.insertSeparator(index); }
/** * Returns a menu with items that control this track. * * @param trackerPanel the tracker panel * @return a menu */ public JMenu getMenu(TrackerPanel trackerPanel) { if (inspectorItem == null) { // create the model inspector item inspectorItem = new JMenuItem(); inspectorItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { positionInspector(); getInspector().setVisible(true); } }); // create the useDefaultRefFrameItem item useDefaultRefFrameItem = new JCheckBoxMenuItem(); useDefaultRefFrameItem.setSelected(!useDefaultReferenceFrame); useDefaultRefFrameItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { setUseDefaultReferenceFrame(!useDefaultRefFrameItem.isSelected()); if (ParticleModel.this.trackerPanel.getCoords() instanceof ReferenceFrame) { lastValidFrame = -1; refreshSteps(); } } }); } inspectorItem.setText( TrackerRes.getString("ParticleModel.MenuItem.InspectModel")); // $NON-NLS-1$ useDefaultRefFrameItem.setText( TrackerRes.getString("ParticleModel.MenuItem.UseDefaultReferenceFrame")); // $NON-NLS-1$ // assemble the menu JMenu menu = super.getMenu(trackerPanel); // remove unwanted menu items and separators menu.remove(autotrackItem); menu.remove(deleteStepItem); menu.remove(clearStepsItem); menu.remove(lockedItem); menu.remove(autoAdvanceItem); menu.remove(markByDefaultItem); menu.insert(inspectorItem, 0); if (menu.getItemCount() > 1) menu.insertSeparator(1); // // find visible item and insert useDefaultRefFrameItem after it // for (int i=0; i<menu.getMenuComponentCount(); i++) { // if (menu.getMenuComponent(i)==visibleItem) { // menu.insert(useDefaultRefFrameItem, i+1); // break; // } // } // eliminate any double separators Object prevItem = inspectorItem; int n = menu.getItemCount(); for (int j = 1; j < n; j++) { Object item = menu.getItem(j); if (item == null && prevItem == null) { // found extra separator menu.remove(j - 1); j = j - 1; n = n - 1; } prevItem = item; } return menu; }