List of usage examples for javax.swing JMenu addSeparator
public void addSeparator()
From source file:net.chaosserver.timelord.swingui.TimelordMenu.java
/** * Creates the task menu./* w w w . j av a2s . co m*/ * * @return the new task menu */ protected JMenu createTaskMenu() { JMenuItem menuItem; JMenu taskMenu = new JMenu("Task"); taskMenu.setMnemonic(KeyEvent.VK_T); this.add(taskMenu); menuItem = new JMenuItem("New Task", KeyEvent.VK_N); menuItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_N, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); menuItem.setActionCommand(ACTION_ADDTASK); menuItem.addActionListener(this); taskMenu.add(menuItem); menuItem = new JMenuItem("Hide Old Tasks", KeyEvent.VK_H); menuItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_H, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); menuItem.setActionCommand(ACTION_HIDETASK); menuItem.addActionListener(this); taskMenu.add(menuItem); menuItem = new JMenuItem("Unhide Task", KeyEvent.VK_U); menuItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_U, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); menuItem.setActionCommand(ACTION_UNHIDETASK); menuItem.addActionListener(this); taskMenu.add(menuItem); taskMenu.addSeparator(); menuItem = new JMenuItem("Add Time", KeyEvent.VK_A); menuItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_A, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); menuItem.setActionCommand(ACTION_ADDTIME); menuItem.addActionListener(this); taskMenu.add(menuItem); menuItem = new JMenuItem("Find Task", KeyEvent.VK_F); menuItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_F, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); menuItem.setActionCommand(ACTION_FINDTASK); menuItem.addActionListener(this); taskMenu.add(menuItem); return taskMenu; }
From source file:components.MenuSelectionManagerDemo.java
public JMenuBar createMenuBar() { JMenuBar menuBar;//w w w . j a va 2 s.co m JMenu menu, submenu; JMenuItem menuItem; JRadioButtonMenuItem rbMenuItem; JCheckBoxMenuItem cbMenuItem; //Create the menu bar. menuBar = new JMenuBar(); //Build the first menu. 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); //a group of JMenuItems menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T); //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything"); menuItem.addActionListener(this); menu.add(menuItem); ImageIcon icon = createImageIcon("images/middle.gif"); menuItem = new JMenuItem("Both text and icon", icon); menuItem.setMnemonic(KeyEvent.VK_B); menuItem.addActionListener(this); menu.add(menuItem); menuItem = new JMenuItem(icon); menuItem.setMnemonic(KeyEvent.VK_D); menuItem.addActionListener(this); menu.add(menuItem); //a group of radio button menu items 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); rbMenuItem.addActionListener(this); menu.add(rbMenuItem); rbMenuItem = new JRadioButtonMenuItem("Another one"); rbMenuItem.setMnemonic(KeyEvent.VK_O); group.add(rbMenuItem); rbMenuItem.addActionListener(this); menu.add(rbMenuItem); //a group of check box menu items menu.addSeparator(); cbMenuItem = new JCheckBoxMenuItem("A check box menu item"); cbMenuItem.setMnemonic(KeyEvent.VK_C); cbMenuItem.addItemListener(this); menu.add(cbMenuItem); cbMenuItem = new JCheckBoxMenuItem("Another one"); cbMenuItem.setMnemonic(KeyEvent.VK_H); cbMenuItem.addItemListener(this); menu.add(cbMenuItem); //a submenu 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)); menuItem.addActionListener(this); submenu.add(menuItem); menuItem = new JMenuItem("Another item"); menuItem.addActionListener(this); submenu.add(menuItem); menu.add(submenu); //Build second menu in the menu bar. menu = new JMenu("Another Menu"); menu.setMnemonic(KeyEvent.VK_N); menu.getAccessibleContext().setAccessibleDescription("This menu does nothing"); menuBar.add(menu); Timer timer = new Timer(ONE_SECOND, new ActionListener() { public void actionPerformed(ActionEvent evt) { MenuElement[] path = MenuSelectionManager.defaultManager().getSelectedPath(); for (int i = 0; i < path.length; i++) { if (path[i].getComponent() instanceof javax.swing.JMenuItem) { JMenuItem mi = (JMenuItem) path[i].getComponent(); if ("".equals(mi.getText())) { output.append("ICON-ONLY MENU ITEM > "); } else { output.append(mi.getText() + " > "); } } } if (path.length > 0) output.append(newline); } }); timer.start(); return menuBar; }
From source file:MenuSelectionManagerDemo.java
public JMenuBar createMenuBar() { JMenuBar menuBar;// w w w . j av a 2 s . com JMenu menu, submenu; JMenuItem menuItem; JRadioButtonMenuItem rbMenuItem; JCheckBoxMenuItem cbMenuItem; //Create the menu bar. menuBar = new JMenuBar(); //Build the first menu. 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); //a group of JMenuItems menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T); //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything"); menuItem.addActionListener(this); menu.add(menuItem); ImageIcon icon = createImageIcon("1.gif"); menuItem = new JMenuItem("Both text and icon", icon); menuItem.setMnemonic(KeyEvent.VK_B); menuItem.addActionListener(this); menu.add(menuItem); menuItem = new JMenuItem(icon); menuItem.setMnemonic(KeyEvent.VK_D); menuItem.addActionListener(this); menu.add(menuItem); //a group of radio button menu items 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); rbMenuItem.addActionListener(this); menu.add(rbMenuItem); rbMenuItem = new JRadioButtonMenuItem("Another one"); rbMenuItem.setMnemonic(KeyEvent.VK_O); group.add(rbMenuItem); rbMenuItem.addActionListener(this); menu.add(rbMenuItem); //a group of check box menu items menu.addSeparator(); cbMenuItem = new JCheckBoxMenuItem("A check box menu item"); cbMenuItem.setMnemonic(KeyEvent.VK_C); cbMenuItem.addItemListener(this); menu.add(cbMenuItem); cbMenuItem = new JCheckBoxMenuItem("Another one"); cbMenuItem.setMnemonic(KeyEvent.VK_H); cbMenuItem.addItemListener(this); menu.add(cbMenuItem); //a submenu 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)); menuItem.addActionListener(this); submenu.add(menuItem); menuItem = new JMenuItem("Another item"); menuItem.addActionListener(this); submenu.add(menuItem); menu.add(submenu); //Build second menu in the menu bar. menu = new JMenu("Another Menu"); menu.setMnemonic(KeyEvent.VK_N); menu.getAccessibleContext().setAccessibleDescription("This menu does nothing"); menuBar.add(menu); Timer timer = new Timer(ONE_SECOND, new ActionListener() { public void actionPerformed(ActionEvent evt) { MenuElement[] path = MenuSelectionManager.defaultManager().getSelectedPath(); for (int i = 0; i < path.length; i++) { if (path[i].getComponent() instanceof javax.swing.JMenuItem) { JMenuItem mi = (JMenuItem) path[i].getComponent(); if ("".equals(mi.getText())) { output.append("ICON-ONLY MENU ITEM > "); } else { output.append(mi.getText() + " > "); } } } if (path.length > 0) output.append(newline); } }); timer.start(); return menuBar; }
From source file:MenuDemo.java
protected JMenuBar createMenuBar() { final JMenuBar menuBar = new JMenuBar(); JMenu menuFile = new JMenu("File"); menuFile.setMnemonic('f'); JMenuItem menuItem = new JMenuItem("New"); menuItem.setIcon(new ImageIcon("file_new.gif")); menuItem.setMnemonic('n'); ActionListener lst = new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("New"); }/*from w ww. j a v a 2 s .c o m*/ }; menuItem.addActionListener(lst); menuFile.add(menuItem); menuItem = new JMenuItem("Open..."); menuItem.setIcon(new ImageIcon("file_open.gif")); menuItem.setMnemonic('o'); lst = new ActionListener() { public void actionPerformed(ActionEvent e) { MenuDemo.this.repaint(); if (fileChooser.showOpenDialog(MenuDemo.this) != JFileChooser.APPROVE_OPTION) return; System.out.println(fileChooser.getSelectedFile()); } }; menuItem.addActionListener(lst); menuFile.add(menuItem); menuItem = new JMenuItem("Save..."); menuItem.setIcon(new ImageIcon("file_save.gif")); menuItem.setMnemonic('s'); lst = new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Save..."); } }; menuItem.addActionListener(lst); menuFile.add(menuItem); menuFile.addSeparator(); menuItem = new JMenuItem("Exit"); menuItem.setMnemonic('x'); lst = new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }; menuItem.addActionListener(lst); menuFile.add(menuItem); menuBar.add(menuFile); ActionListener fontListener = new ActionListener() { public void actionPerformed(ActionEvent e) { updateMonitor(); } }; JMenu mFont = new JMenu("Font"); mFont.setMnemonic('o'); ButtonGroup group = new ButtonGroup(); menus = new JMenuItem[FontNames.length]; for (int i = 0; i < FontNames.length; i++) { int m = i + 1; menus[i] = new JRadioButtonMenuItem(m + " " + FontNames[i]); boolean selected = (i == 0); menus[i].setSelected(selected); menus[i].setMnemonic('1' + i); menus[i].setFont(fontArray[i]); menus[i].addActionListener(fontListener); group.add(menus[i]); mFont.add(menus[i]); } mFont.addSeparator(); boldMenuItem = new JCheckBoxMenuItem("Bold"); boldMenuItem.setMnemonic('b'); Font fn = fontArray[1].deriveFont(Font.BOLD); boldMenuItem.setFont(fn); boldMenuItem.setSelected(false); boldMenuItem.addActionListener(fontListener); mFont.add(boldMenuItem); italicMenuItem = new JCheckBoxMenuItem("Italic"); italicMenuItem.setMnemonic('i'); fn = fontArray[1].deriveFont(Font.ITALIC); italicMenuItem.setFont(fn); italicMenuItem.setSelected(false); italicMenuItem.addActionListener(fontListener); mFont.add(italicMenuItem); menuBar.add(mFont); return menuBar; }
From source file:com.nbt.TreeFrame.java
protected JMenuBar createMenuBar() { JMenuBar menuBar = new JMenuBar(); JMenu menuFile = new JMenu("File"); Action[] fileActions = { /* newAction, */browseAction, saveAction, /* saveAsAction, */refreshAction, exitAction };/*from w w w . jav a2 s . co m*/ for (Action action : fileActions) menuFile.add(new JMenuItem(action)); menuBar.add(menuFile); JMenu menuEdit = new JMenu("Edit"); Action[] editActions = { cutAction, copyAction, pasteAction, null, deleteAction, null, addByteAction, addShortAction, addIntAction, addLongAction, addFloatAction, addDoubleAction, addByteArrayAction, addStringAction, addListAction, addCompoundAction }; for (Action action : editActions) { if (action == null) { menuEdit.addSeparator(); } else { menuEdit.add(new JMenuItem(action)); } } menuBar.add(menuEdit); JMenu menuView = new JMenu("View"); menuView.add(new JMenuItem(openAction)); menuBar.add(menuView); JMenu menuHelp = new JMenu("Help"); menuHelp.add(new JMenuItem(helpAction)); menuBar.add(menuHelp); return menuBar; }
From source file:com.diversityarrays.kdxplore.KDXploreFrame.java
private JMenu createMainMenu(RunMode runMode) { JMenu mainMenu = new JMenu(Msg.MENU_FILE()); mainMenu.add(connectDisconnectActions.connectAction); mainMenu.add(connectDisconnectActions.disconnectAction); mainMenu.addSeparator(); KDClientUtils.initAction(ImageId.SETTINGS_24, settingsAction, Msg.TOOLTIP_CHANGE_PREFS(), true); mainMenu.add(settingsAction);// w w w .ja va 2 s. c om mainMenu.addSeparator(); mainMenu.add(updateAction); mainMenu.addSeparator(); mainMenu.add(exitAction); return mainMenu; }
From source file:adams.gui.tools.FileMonitorPanel.java
/** * Creates a menu bar (singleton per panel object). Can be used in frames. * * @return the menu bar//from www. j ava 2 s.c om */ @Override public JMenuBar getMenuBar() { JMenuBar result; JMenu menu; JMenu submenu; JMenuItem menuitem; if (m_MenuBar == null) { result = new JMenuBar(); // File menu = new JMenu("File"); result.add(menu); menu.setMnemonic('F'); menu.addChangeListener((ChangeEvent e) -> updateMenu()); menuitem = new JMenuItem("Open...", GUIHelper.getIcon("open.gif")); menuitem.setMnemonic('O'); menuitem.setAccelerator(GUIHelper.getKeyStroke("ctrl pressed O")); menuitem.addActionListener((ActionEvent e) -> open()); menu.add(menuitem); m_MenuItemOpen = menuitem; // File/Recent files submenu = new JMenu("Open recent"); menu.add(submenu); m_RecentFilesHandler = new RecentFilesHandler<>(SESSION_FILE, 5, submenu); m_RecentFilesHandler.addRecentItemListener(new RecentItemListener<JMenu, File>() { public void recentItemAdded(RecentItemEvent<JMenu, File> e) { // ignored } public void recentItemSelected(RecentItemEvent<JMenu, File> e) { open(e.getItem()); } }); m_MenuItemOpenRecent = submenu; menu.addSeparator(); menuitem = new JMenuItem("Close", GUIHelper.getIcon("exit.png")); menuitem.setMnemonic('C'); menuitem.setAccelerator(GUIHelper.getKeyStroke("ctrl pressed Q")); menuitem.addActionListener((ActionEvent e) -> close()); menu.add(menuitem); m_MenuItemClose = menuitem; // update menu m_MenuBar = result; updateMenu(); } else { result = m_MenuBar; } return result; }
From source file:com.mgmtp.perfload.loadprofiles.ui.AppFrame.java
private void initMenuBar(final JMenuBar menuBar) { JMenu menu = new JMenu("File"); menu.setMnemonic('F'); menu.add(fileNewLoadProfileConfigAction); menu.add(fileOpenLoadProfileConfigAction); menu.addSeparator(); menu.add(fileSaveLoadProfileConfigAction); menu.add(fileSaveLoadProfileConfigAsAction); menu.addSeparator();/*www. j a v a 2 s .co m*/ menu.add(new FileExitAction(eventBus)); menuBar.add(menu); menu = new JMenu("Tools"); menu.setMnemonic('T'); menu.add(toolsAddStairsAction); menu.add(toolsAddOneTimeAction); menu.add(toolsAddMarkerAction); menu.add(toolsDeleteLoadProfileEntityAction); menu.addSeparator(); menu.add(toolsExportEventListAction); menu.addSeparator(); menu.add(toolsSettingsAction); menuBar.add(menu); menu = new JMenu("Help"); menu.setMnemonic('H'); menu.add(helpAboutAction); menuBar.add(menu); }
From source file:com.haulmont.cuba.desktop.sys.MenuBuilder.java
private void createSubMenu(JMenu jMenu, MenuItem item) { List<MenuItem> itemChildren = new ArrayList<>(item.getChildren()); CollectionUtils.filter(itemChildren, object -> object.isPermitted(userSession)); List<MenuItemContainer> items = new ArrayList<>(); // prepare menu items for (MenuItem child : itemChildren) { if (child.getChildren().isEmpty()) { if (child.isSeparator()) { items.add(new MenuItemContainer()); } else { JMenuItem jMenuItem = new JMenuItem(menuConfig.getItemCaption(child.getId())); jMenuItem.setName(child.getId()); assignCommand(jMenuItem, child); assignShortcut(jMenuItem, child); items.add(new MenuItemContainer(jMenuItem)); }/* w ww . j a va 2 s . c o m*/ } else { JMenu jChildMenu = new JMenu(menuConfig.getItemCaption(child.getId())); createSubMenu(jChildMenu, child); if (!isMenuEmpty(jChildMenu)) { items.add(new MenuItemContainer(jChildMenu)); } } } // remove unnecessary separators if (!items.isEmpty()) { Iterator<MenuItemContainer> iterator = items.iterator(); JMenuItem menuItem = getNextMenuItem(iterator); boolean useSeparator = false; while (menuItem != null) { if (useSeparator) jMenu.addSeparator(); jMenu.add(menuItem); useSeparator = false; menuItem = null; if (iterator.hasNext()) { MenuItemContainer itemContainer = iterator.next(); if (!itemContainer.isSeparator()) menuItem = itemContainer.getMenuItem(); else { menuItem = getNextMenuItem(iterator); useSeparator = true; } } } } }
From source file:IntroExample.java
public IntroExample() { JMenu fileMenu = new JMenu("File"); JMenu editMenu = new JMenu("Edit"); JMenu otherMenu = new JMenu("Other"); JMenu subMenu = new JMenu("SubMenu"); JMenu subMenu2 = new JMenu("SubMenu2"); // Assemble the File menus with mnemonics ActionListener printListener = new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("Menu item [" + event.getActionCommand() + "] was pressed."); }//from w ww .j a v a 2s . c om }; for (int i = 0; i < fileItems.length; i++) { JMenuItem item = new JMenuItem(fileItems[i], fileShortcuts[i]); item.addActionListener(printListener); fileMenu.add(item); } // Assemble the File menus with keyboard accelerators for (int i = 0; i < editItems.length; i++) { JMenuItem item = new JMenuItem(editItems[i]); item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i], Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false)); item.addActionListener(printListener); editMenu.add(item); } // Insert a separator in the Edit Menu in Position 1 after "Undo" editMenu.insertSeparator(1); // Assemble the submenus of the Other Menu JMenuItem item; subMenu2.add(item = new JMenuItem("Extra 2")); item.addActionListener(printListener); subMenu.add(item = new JMenuItem("Extra 1")); item.addActionListener(printListener); subMenu.add(subMenu2); // Assemble the Other Menu itself otherMenu.add(subMenu); otherMenu.add(item = new JCheckBoxMenuItem("Check Me")); item.addActionListener(printListener); otherMenu.addSeparator(); ButtonGroup buttonGroup = new ButtonGroup(); otherMenu.add(item = new JRadioButtonMenuItem("Radio 1")); item.addActionListener(printListener); buttonGroup.add(item); otherMenu.add(item = new JRadioButtonMenuItem("Radio 2")); item.addActionListener(printListener); buttonGroup.add(item); otherMenu.addSeparator(); otherMenu.add(item = new JMenuItem("Potted Plant", new ImageIcon("image.gif"))); item.addActionListener(printListener); // Finally, add all the menus to the menu bar add(fileMenu); add(editMenu); add(otherMenu); }