Java JMenuItem getIconPlaceholderWidth(final JMenuItem menuItem, final boolean alignTextToMenuIcons)

Here you can find the source of getIconPlaceholderWidth(final JMenuItem menuItem, final boolean alignTextToMenuIcons)

Description

Returns maximum icon width for this menu item.

License

Open Source License

Parameter

Parameter Description
menuItem menu item to process
alignTextToMenuIcons whether menu item text should be aligned to icons or not

Return

maximum icon width for this menu item

Declaration

public static int getIconPlaceholderWidth(final JMenuItem menuItem, final boolean alignTextToMenuIcons) 

Method Source Code


//package com.java2s;
/*/*w ww  . ja  v a 2s. c  o  m*/
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.*;
import java.awt.*;

public class Main {
    /**
     * Returns maximum icon width for this menu item.
     * It might take into account other menu items within popup menu.
     *
     * @param menuItem             menu item to process
     * @param alignTextToMenuIcons whether menu item text should be aligned to icons or not
     * @return maximum icon width for this menu item
     */
    public static int getIconPlaceholderWidth(final JMenuItem menuItem, final boolean alignTextToMenuIcons) {
        if (alignTextToMenuIcons && menuItem.getParent() instanceof JPopupMenu) {
            int max = 0;
            final JPopupMenu popupMenu = (JPopupMenu) menuItem.getParent();
            for (int i = 0; i < popupMenu.getComponentCount(); i++) {
                final Component component = popupMenu.getComponent(i);
                if (component instanceof JMenuItem) {
                    final JMenuItem otherItem = (JMenuItem) component;
                    if (otherItem.getIcon() != null) {
                        max = Math.max(max, otherItem.getIcon().getIconWidth());
                    } else if (component instanceof JCheckBoxMenuItem
                            || component instanceof JRadioButtonMenuItem) {
                        max = Math.max(max, 16);
                    }
                }
            }
            return max;
        } else {
            final Icon icon = menuItem.getIcon();
            return icon != null ? icon.getIconWidth() : 0;
        }
    }
}

Related

  1. createJMenuItem(Action action)
  2. createJMenuItem(JMenu menu, String text, int mnemonic, int acceleratorKey, String toolTip, ActionListener actionListener)
  3. createMenu(JMenuItem[] items, String name, String description, int mnemonic)
  4. exit(JMenuItem anItem)
  5. getForeground(final BasicMenuItemUI ui, final JMenuItem menuItem)
  6. getInvoker(final JMenuItem menuItem)
  7. getJMenuItem(String name, JMenuBar jmenubar)
  8. getMenuItemParent(JMenuItem menuItem)
  9. getTopLevelMenu(JMenuItem menuitem)