Here you can find the source of getIconPlaceholderWidth(final JMenuItem menuItem, final boolean alignTextToMenuIcons)
Parameter | Description |
---|---|
menuItem | menu item to process |
alignTextToMenuIcons | whether menu item text should be aligned to icons or not |
public static int getIconPlaceholderWidth(final JMenuItem menuItem, final boolean alignTextToMenuIcons)
//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; } } }