javaswtjface

How to keep @ character in SWT menu item text


I built a Java application using SWT and JFace (on Windows). Here I would like to put an email address as text in a menu item and use the following action class:

private class MyAction extends Action {
    public MyAction() {
        super("xyz@gmail.com");
    }

    @Override
    public void run() {
        // code to execute
    }
}

and of course:

protected MenuManager createMenuManager() {
    MenuManager menuBar = new MenuManager();
    MenuManager myMenu = new MenuManager("Menu");
    myMenu.add(new MyAction());
    menuBar.add(myMenu);
    return menuBar;
}

What confuses me a bit is that when the menu is displayed, the "at" character is missing and a space is shown in its place instead.

Can I somehow keep the @ character?

I've already tried putting a double backslash in front of the @ character or replacing the @ character with the Unicode notation \u0040 - neither of which helped.


Solution

  • This appears to be the "accelerator" text handling in the Action code which converts anything following a tab or @ if there is no tab to a separate part of the menu.

    Since the code first looks for a tab before it looks for @ using:

    super("xyz@gmail.com\t");  // Added tab at end
    

    seems to work.

    You could also use \uFF20 "large commercial @" or \uFE6B "small commercial @" if the font being used supports them.

    Alternatively just using Menu, MenuItem and listeners rather than MenuManager also works.