3

I'm having some trouble getting the underlines to show up consistently for mnemonics on context menus in a Java Swing application that's running on Windows XP.

If I right click the mouse, the underlines don't show up on the popup menu -- that is fine, as such behavior is consistent with other Windows applications.

But if bring up the popup menu with the menu key (typically beside the right Windows key) the underlines don't show up for my Swing application, whereas they show up for standard Windows applications like Wordpad and Explorer and Control Panel.

The only way I can get the underlines to show is if I hold down Alt while right-clicking the mouse. Which is kind of useless, since if somebody already has their hand on the mouse to right-click they won't want to use the keyboard to select something on the popup.

Is it possible to get the underlines to display in Swing when the context menu is called up from the menu key? Without writing my own Look & Feel library?

Gigatron
  • 1,995
  • 6
  • 20
  • 27

1 Answers1

0

Good question. I just tried this on OS X, and I do not get the underlined letters either. Just like you I get them by hitting the alt button (not during click, but when my pop-up menu is shown).

However, on OS X I cannot remember any pop-up which contains underlined letters. I just checked some of the default applications and none of them have a pop-up menu with underlined items. A quick Google search indicated this as well. So in this case, the look-and-feel is consistent with the OS.

After some more googling, I found the following topic which suggest that in Windows there is an option to hide the mnemonics by default, and only show them when you press alt (which you need to press anyway to use the mnemonic if I remember correctly from my windows days). You might want to try that.

Anyhow, here is an SSCCE allowing windows users to quickly test this:

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

public class MnemonicTest {
  public static JFrame createUI(){
    JFrame testFrame = new JFrame(  );

    testFrame.add( createLabelWithPopupMenu() );

    testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    testFrame.pack();
    return testFrame;
  }

  private static JLabel createLabelWithPopupMenu(){
    JLabel result = new JLabel( "Right-click me" );
    result.setComponentPopupMenu( createPopupMenu() );
    return result;
  }



  private static JPopupMenu createPopupMenu(){
    JPopupMenu popupMenu = new JPopupMenu(  );
    popupMenu.add( createAction() );
    return popupMenu;
  }

  private static Action createAction(){
    AbstractAction result = new AbstractAction() {
      @Override
      public void actionPerformed( ActionEvent e ) {
        System.out.println( "MnemonicTest.actionPerformed" );
      }
    };
    result.putValue( Action.MNEMONIC_KEY, KeyEvent.VK_A );
    result.putValue( Action.NAME, "Action" );
    return result;
  }

  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        createUI().setVisible( true );
      }
    } );
  }
}
Robin
  • 36,233
  • 5
  • 47
  • 99