Question

On my OSX application I have a JMenubar with JMenus and Actions

editMenu= new JMenu("EDIT);
editMenu.add(new CopyAction());
editMenu.add(new PasteAction());

but I want to add menu to the dock which I can with Apple extensions and:

Application.getApplication().setDockMenu(java.awt.PopupMenu popupMenu)

Note this is awt from than swing, so I need to add MenuItems rather than JMenuItems

public class DockMenu extends PopupMenu
{
    public DockMenu()
    {
        add(createMenuItemFromAction(new CopyAction()));
        add(createMenuItemFromAction(new PasteAction()));
    }

    public MenuItem createMenuItemFromAction(Action action)
    {
        MenuItem mi = new MenuItem();
        //TODO How do I convert        
    }
}

But Im really not clear how do this conversion, as menuitem doent use actions

Was it helpful?

Solution

An Action also implements ActionListener so you shouldn't have to do anything special. Just add the Action to the MenuItem

You will manually need to set the text and short cut of the MenuItem as you lose this behaviour of the Action. But you can get this information directly from the Action.

public MenuItem createMenuItemFromAction(Action action)
{
    MenuItem mi = new MenuItem(action.getValue(Action.NAME));
    mi.addActionListener( action );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top