United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6458123 Bugs in menu item layout
JDK-6458123 : Bugs in menu item layout

Details
Type:
Bug
Submit Date:
2006-08-08
Status:
Closed
Updated Date:
2011-05-18
Project Name:
JDK
Resolved Date:
2011-05-18
Component:
client-libs
OS:
linux,generic,windows_xp
Sub-Component:
javax.swing
CPU:
x86,generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
6,6u10,6u18,6-pool,7
Fixed Versions:

Related Reports
Backport:
Backport:
Duplicate:
Duplicate:
Duplicate:
Duplicate:
Duplicate:
Duplicate:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:

Sub Tasks

Description
JDK version: mustang b96
Platform: Windows XP SP2

There are several bugs in menu item layout (see the attached test case MenuItemLayoutProblems):

1. Accelerator texts are layouted in a wrong way if menu orientation is Right-to-Left. See the "First R2L" menu ("Text and Accelerator" and "CheckBox, icon and Accelerator" items).

2. If there are items with the "CENTER" text alignment, menu item preferred size is wider, than must be. See the "Second L2R" and "Second R2L" menus.

3. If icon is wider than text and text alignment is the "CENTER", menu item is layouted in a wrong way. See the "Second L2R" and "Second R2L" menus (the "small text" item).

4. If the horizontal alignment of an menu item differs from the default (LEADING), text and icon of all menu items in the same menu are out of view. See the "Third L2R" and "Third R2L" menus.

5. Under GTK LaF icons of check box and radio menu items are shifted by couple of pixels relative to icons of other menu items. Select GTK LaF and see the "First L2R" and "First R2L" menus.

6. Under GTK LaF the accelerator text sometimes overlaps with the text of menu item. Select GTK LaF and see the "Second L2R" menu ("Radio Button, icon at the center and below text" and "Check Box, icon at the center and above text" items)
There is another user reported bug: http://forums.java.net/jive/thread.jspa?messageID=188078&#188078

One of Substance users has reported a regression behaviour on menu items.

The test case is below. If run under 5.0, the texts are not aligned (usual behaviour). Under 6.0, the texts are aligned, but the icon of the first menu item overlaps with the text of that item. Note that this test case requires menu items with icons of different size + the smaller one being installed on checkbox menu item.

In addition, when this test is run in WindowsLookAndFeel, the text alignment and icon alignment is OK (no overlapping), but the check mark of radio button menu item has (hard) square bottom left corner.

Most likely it is a regression of the fix for 4729669 (1.4 REGRESSION: Text edge of different types of JMenuItems are not aligned) introduced in Mustang.

=============Source start===============
import java.awt.*;

import javax.swing.*;
import javax.swing.plaf.metal.MetalLookAndFeel;

public class NewBugTest extends JFrame {
    NewBugTest() {
        JMenuBar jmb = new JMenuBar();
        JMenu menu = new JMenu("menu");
        menu.add(new JMenuItem("item1", new Icon() {
            public int getIconHeight() {
                return 16;
            }

            public int getIconWidth() {
                return 16;
            }

            public void paintIcon(Component c, Graphics g, int x, int y) {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setColor(Color.red);
                g2.fillRect(x, y, 15, 15);
                g2.dispose();
            }
        }));
        menu.add(new JCheckBoxMenuItem("item2", new Icon() {
            public int getIconHeight() {
                return 8;
            }

            public int getIconWidth() {
                return 8;
            }

            public void paintIcon(Component c, Graphics g, int x, int y) {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setColor(Color.red);
                g2.fillRect(x, y, 7, 7);
                g2.dispose();
            }
        }));
        JRadioButtonMenuItem menuItem3 = new JRadioButtonMenuItem("item3");
        menuItem3.setSelected(true);
        menu.add(menuItem3);
        jmb.add(menu);

        this.setJMenuBar(jmb);

        this.setSize(200, 200);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(new MetalLookAndFeel());
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new NewBugTest().setVisible(true);
            }
        });
    }
}
=============Source end===============
The description of a duplicate bug (6745220):

FULL PRODUCT VERSION :
Any version of Tiger and Mustang

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
The menu accelerators under RTL layout are not aligned correctly.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create an application with different menu items and set frame.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
The accelerator texts are not aligned and many of them are cur by the window itself. See many screenshots of Metal, Windows and third-party LAFs that inherit them at http://weblogs.java.net/blog/kirillcool/archive/2006/02/aligning_menu_i_1.html

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Accelerator texts are aligned
ACTUAL -
Accelerator texts are partlt cut and not aligned

REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
As described in https://looks.dev.java.net/issues/show_bug.cgi?id=111 the line

 accOffset = maxValue - acceleratorRect.width;

(613 in Tiger) should be replaced by

 accOffset = menuItem.getComponentOrientation().isLeftToRight()
     ? maxValue - acceleratorRect.width
     : acceleratorRect.width - maxValue;

                                    

Comments
EVALUATION

The problem is in the BasicMenuItemUI.layoutMenuItem() and BasicMenuItemUI.getPreferredMenuItemSize() methods. Now X coordinates of text and icon are calculated through offsets. A better approach is to calculate and use maximal widths of the menu item elements. 

Also a better approach of menu item layouting is to align all the elements in columns. In the case, when a user manually sets alignment or text alignment, the good idea is to use SwingUtilities.layoutCompoundLabel() for layout of icon and text.

One of possible solutions is suggested here: http://sa.sfbay.sun.com/projects/swing_data/mustang/6438430.2/
                                     
2006-08-08
SUGGESTED FIX

Webrevs: http://sa.sfbay.sun.com/projects/swing_data/7/6458123/
                                     
2007-07-18
WORK AROUND

There is no workaround.
                                     
2007-07-18
SUGGESTED FIX

While I was writing the fix, SynthMenuItemUI was significantly changed. To reduce complexity of my fix I decided to divide it into two parts: common (all Look and Feels, except GTK) and GTK part.
This fix includes only common part. I will create a new CR for GTK shortly.
                                     
2007-07-18



Hardware and Software, Engineered to Work Together