Creates a new JMenuItem and configures it for use in a popup menu. - Java Swing

Java examples for Swing:JMenu

Description

Creates a new JMenuItem and configures it for use in a popup menu.

Demo Code

/*//from ww w . j  av  a2s . c o  m
 * Copyright (c) 2007 BUSINESS OBJECTS SOFTWARE LIMITED
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *  
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *  
 *     * Neither the name of Business Objects nor the names of its contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.SystemColor;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import javax.swing.JViewport;
import javax.swing.KeyStroke;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.plaf.OptionPaneUI;
import javax.swing.plaf.basic.BasicBorders;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicOptionPaneUI;
import javax.swing.plaf.basic.BasicToggleButtonUI;
import javax.swing.text.JTextComponent;
import javax.swing.text.Keymap;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;

public class Main{
    private static final boolean JUSTIFY_MENU_ITEM_HACK = System
            .getProperty("java.version").startsWith("1.5")
            && !(lowerCaseOSName.startsWith("mac os x") || lowerCaseOSName
                    .endsWith("vista"));
    /**
     * Creates a new JMenuItem and configures it for use in a popup menu.
     * @param action Action - the action used to configure the JMenuItem
     * @return JMenuItem a new menu item for the action.
     */
    public static JMenuItem makeNewMenuItem(Action action) {
        JMenuItem newMenuItem = new JMenuItem(action);
        return fixMenuItem(newMenuItem);
    }
    /**
     * Fix up the given menu item so that it is justified correctly, has the associated icon, has no tooltip.
     * @param menuItem the menu item to fix.
     * @return the menu item that was fixed.  ie. the menuItem param.
     */
    public static JMenuItem fixMenuItem(JMenuItem menuItem) {

        // HACK: Workaround for left-justifying icons. Sun Bug ID's: 4199382, 4203899.
        // Hack seems to bugger up mac os x
        // On Vista it makes the icon shift left enough so that you can't see most of it.
        // Fixed in Java 6.

        if (JUSTIFY_MENU_ITEM_HACK) {
            // Get the icon, calculate its width.
            Icon buttonIcon = menuItem.getIcon() != null ? menuItem
                    .getIcon()
                    : menuItem.getAction() != null ? (Icon) menuItem
                            .getAction().getValue(Action.SMALL_ICON) : null;
            int iconWidth = (buttonIcon == null) ? -(menuItem
                    .getIconTextGap()) : buttonIcon.getIconWidth();

            Insets insets = menuItem.getInsets();
            insets.left -= iconWidth;
            menuItem.setMargin(insets);
        }

        // Menu items should not have tooltips, so clear it in case the action specifies one
        menuItem.setToolTipText(null);

        return menuItem;
    }
}

Related Tutorials