JDK-6385358 : JMenuitem's text overlaps with shortcut keys
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-02-14
  • Updated: 2017-05-23
Description
FULL PRODUCT VERSION :
1.4.2_10
J2SE 5.0 Update 6 and mustang b69

ADDITIONAL OS VERSION INFORMATION :
Windows XP Professional SP1+

EXTRA RELEVANT SYSTEM CONFIGURATION :
Not only Windows L&F

A DESCRIPTION OF THE PROBLEM :
the text of a JMenuitem will overlap with the shortcut key when set the text alignment to SwingConstants.RIGHT.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
package testbug;

import java.awt.Dimension;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

public class TestFontBug extends JFrame {
    private JMenuBar menuBar = new JMenuBar();

    private JMenu menuFile = new JMenu();

    private JMenuItem jMenuItem1 = new JMenuItem();

    public TestFontBug() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setJMenuBar( menuBar );
        this.getContentPane().setLayout( null );
        this.setSize( new Dimension(400, 300) );
        this.setTitle( "Bug For U" );
        menuFile.setText( "File" );
        jMenuItem1.setText("Test Shortcut");
        jMenuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.SHIFT_MASK | Event.CTRL_MASK | Event.ALT_MASK, false));
        jMenuItem1.setHorizontalAlignment(SwingConstants.RIGHT);
        menuFile.add(jMenuItem1);
        menuBar.add(menuFile);
    }

    void fileExit_ActionPerformed(ActionEvent e) {
        System.exit(0);
    }
    
    public static void main(String[] argv){
        TestFontBug bug = new TestFontBug();
        bug.setVisible(true);
    }
}



REPRODUCIBILITY :
This bug can be reproduced always.

Comments
EVALUATION JMenuItems with setHorizontalAlignment(SwingConstants.RIGHT) has never worked correctly, I think we should document that JMenuItems doesn't support such alignments (It doesnt seem to make sence using this alignment for menu items)
17-03-2006

EVALUATION A have tested JDK 1.3.1_16 for the bug. Text does not overlap with shortcut keys, but it is shown at the right side of keys (instead of the left side) and it is partially hidden.
17-03-2006

EVALUATION The bug
17-03-2006

EVALUATION I have tested JDK 1.3.1_16 for the bug. The text also overlaps.
17-03-2006

EVALUATION Yuck! In 1.5.0 the text overlaps the shortcut text. In the latest Mustang build, ONLY the shortcut text appears (without the menu item text).
14-02-2006