Java Utililty Methods JMenuItem

List of utility methods to do JMenuItem

Description

The list of methods to do JMenuItem are organized into topic(s).

Method

voidlinkMenuItem(final JMenuItem master, final JMenuItem slave)
Forces slave menu item to reflect master menu item using a property change listener.
final JMenuItem source = master, dest = slave;
source.addPropertyChangeListener(new PropertyChangeListener() {
    @Override
    public void propertyChange(final PropertyChangeEvent e) {
        syncMenuItem(source, dest);
});
voidpintaBarraMenu(Graphics g, JMenuItem menuItem, Color bgColor)
Esta funcion se usa para pintar la barra de seleccion de los menus.
ButtonModel model = menuItem.getModel();
Color oldColor = g.getColor();
int menuWidth = menuItem.getWidth();
int menuHeight = menuItem.getHeight();
if (menuItem.isOpaque()) {
    g.setColor(menuItem.getBackground());
    g.fillRect(0, 0, menuWidth, menuHeight);
if ((menuItem instanceof JMenu && !(((JMenu) menuItem).isTopLevelMenu()) && model.isSelected())
        || model.isArmed()) {
    RoundRectangle2D.Float boton = new RoundRectangle2D.Float();
    boton.x = 1;
    boton.y = 0;
    boton.width = menuWidth - 3;
    boton.height = menuHeight - 1;
    boton.arcwidth = 8;
    boton.archeight = 8;
    GradientPaint grad = new GradientPaint(1, 1, getBrilloMenu(), 0, menuHeight, getSombraMenu());
    Graphics2D g2D = (Graphics2D) g;
    g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setColor(bgColor);
    g2D.fill(boton);
    g.setColor(bgColor.darker());
    g2D.draw(boton);
    g2D.setPaint(grad);
    g2D.fill(boton);
    g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT);
g.setColor(oldColor);
booleanrecursiveUpdateOrInsertMenuItem(MenuElement menu, JMenuItem menuItem, boolean hideOnUpdate)
recursive Update Or Insert Menu Item
boolean updated = false;
for (MenuElement menuEl : menu.getSubElements()) {
    if (menuEl instanceof JMenuItem) {
        JMenuItem subMenuItem = (JMenuItem) menuEl;
        String actionCommand = subMenuItem.getActionCommand();
        if (actionCommand.equals(menuItem.getActionCommand())) {
            if (hideOnUpdate) {
                subMenuItem.setVisible(false);
...
booleanreplaceMenuItem(@Nonnull JMenuItem orginalMenuItem, @Nonnull JMenuItem replacementMenuItem)
replace a menu item in its parent with another menu item

(at the same position in the parent menu)

boolean result = false; 
Container container = orginalMenuItem.getParent();
if (container != null) {
    int index = container.getComponentZOrder(orginalMenuItem);
    if (index > -1) {
        container.remove(orginalMenuItem);
        container.add(replacementMenuItem, index);
        result = true;
...
voidsetAccelerator(JMenuItem menuItem, int key, int mask)
set Accelerator
menuItem.setAccelerator(KeyStroke.getKeyStroke(key, mask));
voidsetCtrlAccelerator(final JMenuItem jmi, final char accelerator)
Sets the accelerator from the given menuitem and the given character with the CTRL.
final KeyStroke ks = KeyStroke.getKeyStroke(accelerator, Event.CTRL_MASK);
jmi.setAccelerator(ks);
voidsetCtrlAccelerator(JMenuItem jmi, char accelerator)
Sets the accelerator from the given menuitem and the given character with the CTRL.
KeyStroke ks = KeyStroke.getKeyStroke(accelerator, Event.CTRL_MASK);
jmi.setAccelerator(ks);
voidsetEnabled(boolean enabled, JMenuItem... items)
set Enabled
for (JMenuItem item : items)
    item.setEnabled(enabled);
voidsetKeystroke(JMenuItem m, int key)
set Keystroke
if (key > 0) {
    if (key != KeyEvent.VK_DELETE)
        m.setAccelerator(KeyStroke.getKeyStroke(key, Event.CTRL_MASK, false));
    else
        m.setAccelerator(KeyStroke.getKeyStroke(key, 0, false));
voidsetMnemonic(JMenuItem item, String label, int index)
set Mnemonic
try {
    char mnemonic = label.charAt(index);
    item.setMnemonic(mnemonic);
    item.setDisplayedMnemonicIndex(index);
} catch (IndexOutOfBoundsException e) {