Here you can find the source of layoutMenuItem(JMenuItem menuItem, FontMetrics fm, String text, FontMetrics fmAccel, String acceleratorText, Icon icon, Icon checkIcon, Icon arrowIcon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewRect, Rectangle iconRect, Rectangle textRect, Rectangle acceleratorRect, Rectangle checkIconRect, Rectangle arrowIconRect, int textIconGap, int menuItemGap)
private static String layoutMenuItem(JMenuItem menuItem, FontMetrics fm, String text, FontMetrics fmAccel, String acceleratorText, Icon icon, Icon checkIcon, Icon arrowIcon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewRect, Rectangle iconRect, Rectangle textRect, Rectangle acceleratorRect, Rectangle checkIconRect, Rectangle arrowIconRect, int textIconGap, int menuItemGap)
//package com.java2s; import java.awt.FontMetrics; import java.awt.Rectangle; import javax.swing.Icon; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.SwingUtilities; public class Main { /**/*from w ww . j ava 2 s . c om*/ * Compute and return the location of the icons origin, the location of * origin of the text baseline, and a possibly clipped version of the * compound labels string. Locations are computed relative to the viewRect * rectangle. */ private static String layoutMenuItem(JMenuItem menuItem, FontMetrics fm, String text, FontMetrics fmAccel, String acceleratorText, Icon icon, Icon checkIcon, Icon arrowIcon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewRect, Rectangle iconRect, Rectangle textRect, Rectangle acceleratorRect, Rectangle checkIconRect, Rectangle arrowIconRect, int textIconGap, int menuItemGap) { SwingUtilities.layoutCompoundLabel(menuItem, fm, text, icon, verticalAlignment, horizontalAlignment, verticalTextPosition, horizontalTextPosition, viewRect, iconRect, textRect, textIconGap); /* * Initialize the acceelratorText bounds rectangle textRect. If a null * or and empty String was specified we substitute "" here and use * 0,0,0,0 for acceleratorTextRect. */ if ((acceleratorText == null) || acceleratorText.equals("")) { acceleratorRect.width = acceleratorRect.height = 0; acceleratorText = ""; } else { acceleratorRect.width = fmAccel.stringWidth(acceleratorText); acceleratorRect.height = fmAccel.getHeight(); } /* * Initialize the checkIcon bounds rectangle's width & height. */ if (useCheckAndArrow(menuItem)) { if (checkIcon != null) { checkIconRect.width = checkIcon.getIconWidth(); checkIconRect.height = checkIcon.getIconHeight(); } else { checkIconRect.width = checkIconRect.height = 0; } /* * Initialize the arrowIcon bounds rectangle width & height. */ if (arrowIcon != null) { arrowIconRect.width = arrowIcon.getIconWidth(); arrowIconRect.height = arrowIcon.getIconHeight(); } else { arrowIconRect.width = arrowIconRect.height = 0; } } Rectangle labelRect = iconRect.union(textRect); if (menuItem.getComponentOrientation().isLeftToRight()) { textRect.x += menuItemGap; iconRect.x += menuItemGap; // Position the Accelerator text rect acceleratorRect.x = viewRect.x + viewRect.width - arrowIconRect.width - menuItemGap - acceleratorRect.width; // Position the Check and Arrow Icons if (useCheckAndArrow(menuItem)) { checkIconRect.x = viewRect.x + menuItemGap; textRect.x += menuItemGap + checkIconRect.width; iconRect.x += menuItemGap + checkIconRect.width; arrowIconRect.x = viewRect.x + viewRect.width - menuItemGap - arrowIconRect.width; } } else { textRect.x -= menuItemGap; iconRect.x -= menuItemGap; // Position the Accelerator text rect acceleratorRect.x = viewRect.x + arrowIconRect.width + menuItemGap; // Position the Check and Arrow Icons if (useCheckAndArrow(menuItem)) { checkIconRect.x = viewRect.x + viewRect.width - menuItemGap - checkIconRect.width; textRect.x -= menuItemGap + checkIconRect.width; iconRect.x -= menuItemGap + checkIconRect.width; arrowIconRect.x = viewRect.x + menuItemGap; } } // Align the accelertor text and the check and arrow icons vertically // with the center of the label rect. acceleratorRect.y = labelRect.y + (labelRect.height / 2) - (acceleratorRect.height / 2); if (useCheckAndArrow(menuItem)) { arrowIconRect.y = labelRect.y + (labelRect.height / 2) - (arrowIconRect.height / 2); checkIconRect.y = labelRect.y + (labelRect.height / 2) - (checkIconRect.height / 2); } /* * System.out.println("Layout: text="+menuItem.getText()+"\n\tv=" * +viewRect+"\n\tc="+checkIconRect+"\n\ti=" * +iconRect+"\n\tt="+textRect+"\n\tacc=" * +acceleratorRect+"\n\ta="+arrowIconRect+"\n"); */ return text; } private static boolean useCheckAndArrow(JMenuItem menuItem) { boolean b = true; if ((menuItem instanceof JMenu) && (((JMenu) menuItem).isTopLevelMenu())) { b = false; } return b; } }