Java Code Examples for javax.swing.JMenuItem#getIcon()

The following examples show how to use javax.swing.JMenuItem#getIcon() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SeaGlassGraphicsUtils.java    From seaglass with Apache License 2.0 6 votes vote down vote up
static void paintIcon(Graphics g, SeaGlassMenuItemLayoutHelper lh,
                      MenuItemLayoutHelper.LayoutResult lr) {
    if (lh.getIcon() != null) {
        Icon icon;
        JMenuItem mi = lh.getMenuItem();
        ButtonModel model = mi.getModel();
        if (!model.isEnabled()) {
            icon = mi.getDisabledIcon();
        } else if (model.isPressed() && model.isArmed()) {
            icon = mi.getPressedIcon();
            if (icon == null) {
                // Use default icon
                icon = mi.getIcon();
            }
        } else {
            icon = mi.getIcon();
        }

        if (icon != null) {
            Rectangle iconRect = lr.getIconRect();
            SynthIcon.paintIcon(icon, lh.getContext(), g, iconRect.x,
                    iconRect.y, iconRect.width, iconRect.height);
        }
    }
}
 
Example 2
Source File: JMenuItemJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private static String getTextFromIcon(JMenuItem mi) {
    Icon icon = mi.getIcon();
    if (icon != null && icon instanceof ImageIcon) {
        String description = ((ImageIcon) icon).getDescription();
        return getNameFromImageDescription(description);
    }
    return null;
}
 
Example 3
Source File: MenuEditLayer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
void configureMenuItem(final JMenu parent, final JComponent c) {
    if(c instanceof JMenuItem) {
        JMenuItem item = (JMenuItem) c;
        if(!(item.getIcon() instanceof WrapperIcon)) {
            item.setIcon(new WrapperIcon(item.getIcon()));
        }
        installAcceleratorPreview(item);
        item.setBorderPainted(true);
    }
}
 
Example 4
Source File: DropTargetLayer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void drawSubselectedItem(Graphics2D g2, JMenuItem item) {
    Point location = SwingUtilities.convertPoint(item, new java.awt.Point(0, 0), this);
    g2.translate(location.x, location.y);

    int iconGap = item.getIconTextGap();
    int iconLeft = getIconLeft(item);
    int iconWidth = getIconWidth(item);
    int iconHeight = getIconHeight(item);
    int iconTop = (item.getHeight() - iconHeight) / 2;
    int accelWidth = getAcceleratorWidth(item);

    int textWidth = item.getWidth() - iconLeft - iconWidth - iconGap - accelWidth;
    int accelLeft = item.getWidth() - accelWidth;

    // draw bounding boxes
    g2.setColor(Color.LIGHT_GRAY);
    //g2.drawRect(iconLeft, 0, iconWidth-1, item.getHeight()-1);
    //g2.drawRect(textLeft, 0, textWidth-1, item.getHeight()-1);
    //draw the accelerator areaa
    //g2.drawRect(accelLeft, 0, accelWidth - 1, item.getHeight() - 1);

    // draw the selection rectangles
    g2.setStroke(SELECTION_STROKE);
    g2.setColor(SELECTION_COLOR);
    switch (canvas.getCurrentSelectedPortion()) {
        case Icon:
            {
                if (item.getIcon() != null) {
                    g2.drawRect(iconLeft - 1, iconTop - 1, iconWidth + 1, iconHeight + 1);
                }
                break;
            }
        case Text:
            {
                g2.drawRect(iconLeft + iconWidth + iconGap - 1, -1, textWidth + 1, item.getHeight() + 1);
                break;
            }
        case Accelerator:
            {
                if (item instanceof javax.swing.JMenu) {
                    break;
                }
                g2.drawRect(accelLeft - 1, -1, accelWidth + 1, item.getHeight() + 1);
                break;
            }
        case All:
            {
                g2.drawRect(0, 0, item.getWidth() - 1, item.getHeight() - 1);
            }
    }
    g2.translate(-location.x, -location.y);
}
 
Example 5
Source File: DropTargetLayer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static int getIconWidth(JMenuItem item) {
    int iconWidth = item.getIcon() != null ? item.getIcon().getIconWidth() : 0;
    return iconWidth;
}
 
Example 6
Source File: DropTargetLayer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static int getIconHeight(JMenuItem item) {
    int iconHeight = item.getIcon() != null ? item.getIcon().getIconHeight() : 0;
    return iconHeight;
}
 
Example 7
Source File: SeaGlassMenuItemUI.java    From seaglass with Apache License 2.0 4 votes vote down vote up
static Dimension getPreferredMenuItemSize(SeaGlassContext context, SeaGlassContext accContext, boolean useCheckAndArrow, JComponent c,
    Icon checkIcon, Icon arrowIcon, int defaultTextIconGap, String acceleratorDelimiter) {
    JMenuItem b = (JMenuItem) c;
    Icon icon = (Icon) b.getIcon();
    String text = b.getText();
    KeyStroke accelerator = b.getAccelerator();
    String acceleratorText = "";

    if (accelerator != null) {
        int modifiers = accelerator.getModifiers();
        if (modifiers > 0) {
            acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
            acceleratorText += acceleratorDelimiter;
        }
        int keyCode = accelerator.getKeyCode();
        if (keyCode != 0) {
            acceleratorText += KeyEvent.getKeyText(keyCode);
        } else {
            acceleratorText += accelerator.getKeyChar();
        }
    }

    Font font = context.getStyle().getFont(context);
    FontMetrics fm = b.getFontMetrics(font);
    FontMetrics fmAccel = b.getFontMetrics(accContext.getStyle().getFont(accContext));

    resetRects();

    layoutMenuItem(context, fm, accContext, text, fmAccel, acceleratorText, icon, checkIcon, arrowIcon, b.getVerticalAlignment(), b
        .getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect,
        acceleratorRect, checkIconRect, arrowIconRect, text == null ? 0 : defaultTextIconGap, defaultTextIconGap, useCheckAndArrow);
    // find the union of the icon and text rects
    r.setBounds(textRect);
    r = SwingUtilities.computeUnion(iconRect.x, iconRect.y, iconRect.width, iconRect.height, r);
    // To make the accelerator texts appear in a column,
    // find the widest MenuItem text and the widest accelerator text.

    // Get the parent, which stores the information.
    Container parent = b.getParent();

    if (parent instanceof JPopupMenu) {
        SeaGlassPopupMenuUI popupUI = (SeaGlassPopupMenuUI) SeaGlassLookAndFeel.getUIOfType(((JPopupMenu) parent).getUI(),
            SeaGlassPopupMenuUI.class);

        if (popupUI != null) {
            r.width = popupUI.adjustTextWidth(r.width);

            popupUI.adjustAcceleratorWidth(acceleratorRect.width);

            r.width += popupUI.getMaxAcceleratorWidth();
        }
    } else if (parent != null && !(b instanceof JMenu && ((JMenu) b).isTopLevelMenu())) {
        r.width += acceleratorRect.width;
    }

    if (useCheckAndArrow) {
        // Add in the checkIcon
        r.width += checkIconRect.width;
        r.width += defaultTextIconGap;

        // Add in the arrowIcon
        r.width += defaultTextIconGap;
        r.width += arrowIconRect.width;
    }

    r.width += 2 * defaultTextIconGap;

    Insets insets = b.getInsets();
    if (insets != null) {
        r.width += insets.left + insets.right;
        r.height += insets.top + insets.bottom;
    }

    // if the width is even, bump it up one. This is critical
    // for the focus dash line to draw properly
    if (r.width % 2 == 0) {
        r.width++;
    }

    // if the height is even, bump it up one. This is critical
    // for the text to center properly
    if (r.height % 2 == 0) {
        r.height++;
    }
    return r.getSize();
}