List of usage examples for javax.swing JMenuItem getText
public String getText()
From source file:Main.java
public static void main(String[] argv) throws Exception { MenuElement[] path = MenuSelectionManager.defaultManager().getSelectedPath(); if (path.length == 0) { System.out.println("No menus are opened or menu items selected"); }/*w w w. j a va 2 s. c om*/ for (int i = 0; i < path.length; i++) { Component c = path[i].getComponent(); if (c instanceof JMenuItem) { JMenuItem mi = (JMenuItem) c; String label = mi.getText(); } } }
From source file:Main.java
public final static int getMenuItemIndex(JMenu menu, String menuItemText) { int theIdx = -1; for (int i = 0; i < menu.getMenuComponentCount(); i++) { Component c = menu.getMenuComponent(i); if (c instanceof JMenuItem) { JMenuItem mi = (JMenuItem) c; if (mi.getText().equals(menuItemText)) { theIdx = i;/*from ww w . ja v a 2 s . c om*/ i = menu.getMenuComponentCount();// break } } } return theIdx; }
From source file:Main.java
/** Brings the destination menu item into sync with the source item. */ protected static void syncMenuItem(final JMenuItem source, final JMenuItem dest) { final boolean enabled = source.isEnabled(); if (dest.isEnabled() != enabled) dest.setEnabled(enabled);/* w ww . j a v a 2 s.com*/ final int mnemonic = source.getMnemonic(); if (dest.getMnemonic() != mnemonic) dest.setMnemonic(mnemonic); final String text = source.getText(); if (dest.getText() != text) dest.setText(text); final KeyStroke accel = source.getAccelerator(); if (dest.getAccelerator() != accel) dest.setAccelerator(accel); }
From source file:MenuDemo.java
public void itemStateChanged(ItemEvent e) { JMenuItem source = (JMenuItem) (e.getSource()); String s = "Item event detected." + newline + " Event source: " + source.getText() + " (an instance of " + getClassName(source) + ")" + newline + " New state: " + ((e.getStateChange() == ItemEvent.SELECTED) ? "selected" : "unselected"); output.append(s + newline);/* w w w. ja v a2 s .co m*/ output.setCaretPosition(output.getDocument().getLength()); }
From source file:MenuDemo.java
public void actionPerformed(ActionEvent e) { JMenuItem source = (JMenuItem) (e.getSource()); String s = "Action event detected." + newline + " Event source: " + source.getText() + " (an instance of " + getClassName(source) + ")"; output.append(s + newline);/*from ww w. j a v a2 s .co m*/ output.setCaretPosition(output.getDocument().getLength()); }
From source file:components.MenuSelectionManagerDemo.java
public JMenuBar createMenuBar() { JMenuBar menuBar;// w ww . j a v a 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;/*from w ww . j a va2s. c o 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("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:edu.wisc.ssec.mcidasv.control.McIDASVHistogramWrapper.java
/** * Add the default menu items/*from ww w .j a v a 2 s . co m*/ * * @param items List of menu items * * @return The items list */ @Override protected List getPopupMenuItems(List items) { items = super.getPopupMenuItems(items); for (Object o : items) { if (o instanceof JMenuItem) { JMenuItem menuItem = (JMenuItem) o; if ("Properties...".equals(menuItem.getText())) { menuItem.setActionCommand(ChartPanel.PROPERTIES_COMMAND); menuItem.addActionListener(buildHistoPropsListener()); } } } return items; }
From source file:ucar.unidata.idv.control.McVHistogramWrapper.java
/** * Add the default menu items//from w w w . j a v a2 s . c om * * @param items List of menu items * * @return The items list */ @Override public List getPopupMenuItems(List items) { items = super.getPopupMenuItems(items); for (Object o : items) { if (o instanceof JMenuItem) { JMenuItem menuItem = (JMenuItem) o; if ("Properties...".equals(menuItem.getText())) { if (menuItem.getActionListeners().length == 0) { menuItem.setActionCommand(ChartPanel.PROPERTIES_COMMAND); menuItem.addActionListener(buildHistoPropsListener()); } } } } return items; }
From source file:edu.ku.brc.af.ui.db.JAutoCompTextField.java
protected void keyReleasedInternal(KeyEvent ev) { //System.out.println(ev); if (dbAdapter != null) { if (ev.getKeyCode() == JAutoCompComboBox.SEARCH_KEY) { /*lookup(getText()); /*from w w w.j ava 2 s . co m*/ if (!foundMatch) { setText(""); }*/ ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent ae) { JMenuItem mi = (JMenuItem) ae.getSource(); int inx = 0; for (PickListItemIFace pli : dbAdapter.getList()) { String title = pli.getTitle(); if (mi.getText().equals(title)) { setSelectedIndex(inx); break; } inx++; } popupMenu = null; } }; String txt = getText(); int cnt = 0; for (PickListItemIFace pli : dbAdapter.getList()) { String title = pli.getTitle(); if (title.length() >= txt.length() && title.startsWith(txt)) { cnt++; } } if (cnt > 0) { popupMenu = new JPopupMenu(); for (PickListItemIFace pli : dbAdapter.getList()) { String title = pli.getTitle(); if (title.length() >= txt.length() && title.startsWith(txt)) { JMenuItem mi = new JMenuItem(title); setControlSize(mi); popupMenu.add(mi); mi.addActionListener(al); } } Point location = getLocation(); Dimension size = getSize(); popupMenu.show(this, location.x, location.y + size.height); } } else if (ev.getKeyCode() == KeyEvent.VK_ENTER) { addNewItemFromTextField(); } else if (ev.getKeyCode() == KeyEvent.VK_END)// || ev.getKeyCode() == KeyEvent.VK_SHIFT) { setSelectionStart(prevCaretPos); setSelectionEnd(getText().length()); } /*else { char key = ev.getKeyChar(); if (ev.getKeyCode() == KeyEvent.VK_BACK_SPACE) { String s = getText(); if (foundMatch) { //System.out.println("len ["+s.length()+"]"); //System.out.println(s+"["+s.substring(0, s.length()-1)+"]"); setText(s.length() == 0 ? "" : s.substring(0, s.length()-1)); } else { hasChanged = true; } return; } else if ((!(Character.isLetterOrDigit(key) || Character.isSpaceChar(key))) && ev.getKeyCode() != KeyEvent.VK_DELETE) { if (ev.getKeyCode() == KeyEvent.VK_ENTER) { addNewItemFromTextField(); } //System.out.println("Key Code "+ev.getKeyCode()+" Pos: "+getCaretPosition()+" Del: "+KeyEvent.VK_DELETE); if (ev.getKeyCode() == KeyEvent.VK_END)// || ev.getKeyCode() == KeyEvent.VK_SHIFT) { setSelectionStart(prevCaretPos); setSelectionEnd(getText().length()); } return; } else if(ev.getKeyCode() == KeyEvent.VK_DELETE) { foundMatch = false; hasChanged = true; return; } //System.out.println("["+ev.getKeyCode()+"]["+KeyEvent.VK_DELETE+"]"); caretPos = getCaretPosition(); String text = ""; try { text = getText(0, caretPos); } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(JAutoCompTextField.class, ex); ex.printStackTrace(); } lookup(text); }*/ } }