JDK-6568559 : JPopupMenu has incorrect size when it contains Icons and Text JMenuItems
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2007-06-12
  • Updated: 2011-02-16
  • Resolved: 2007-07-12
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_01"
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Server VM (build 1.6.0_01-b06, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux speedy 2.6.20.1mjs #3 SMP Mon May 14 11:22:25 CDT 2007 x86_64 x86_64 x86_64 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
If a JPopupMenu is constructed with an icon and text items, the text items are located to the right of the right margin of the Icon item. The resulting popup menu is much wider than it needs to be, and text items are right-justified.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached program. Click in the window area and observe the popup menu.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A popup menu that is as wide as the icon.
ACTUAL -
A popup menu that is as wide as the icon+text width.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.*;

public class TestBug extends JFrame {

	JPopupMenu jp;
	public TestBug() {
		jp=new JPopupMenu();
		jp.add(new JMenuItem("Test"));
		try {
	        jp.add(new JMenuItem(new ImageIcon(new URL("http://java.sun.com/images/featureart/dwnld_netbeans_off.gif"))));
	    } catch (MalformedURLException e1) {
	        e1.printStackTrace();
        }
		jp.add(new JMenuItem("Test"));
		this.addMouseListener(new MouseAdapter() {

			public void mouseClicked(MouseEvent e) {
	            jp.show(TestBug.this,e.getX(),e.getY());
	            
            }

			public void mouseReleased(MouseEvent e) {
	            jp.setVisible(false);
	            
            }
			
		});
		setMinimumSize(new Dimension(500,500));
		setSize(new Dimension(500,500));
		pack();
	}

public static void main(String[] args) {
	TestBug tb=new TestBug();
	tb.setVisible(true);

}
}
---------- END SOURCE ----------

Release Regression From : 5.0u11
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
EVALUATION Moreover, I wrote a fix for 6458123, which is about to be approved. The fix completely changes the layout algorithm for menu items. The new algorithm also lines up vertically icons in one column and texts in another.
12-07-2007

EVALUATION It is not a bug. It is a feature. Most likely it is a consequence of the fix for 4729669, which was integrated into a JDK6 early build. Now all icons are laid out in one vertical column and all texts are laid out into another column. There is a workaround. For the menu items, which consist of only an icon, you can call: setHorizontalTextPosition(SwingConstants.LEADING); It will make icons and texts lining up in one column. However, it will work only if there is no any menu item which has icon and text simultaneously.
12-07-2007

WORK AROUND There is a workaround. For the menu items, which consist of only an icon, you can call: setHorizontalTextPosition(SwingConstants.LEADING); It will make icons and texts lining up in one column. However, it will work only if there is no any menu item which has icon and text simultaneously.
12-07-2007

EVALUATION It looks like a desired behaviour reassigned to JMenu owner
12-07-2007