javaswinglabeljmenuitemabstract-action

Change an AbstractAction name


I have a JMenuItem bounded to an Action that I can get using item.getAction(). The action name is set when constructing the Action, e.g. using anonymous new AbstractAction(String text, ...). The text field is set according to a ResourceBundle and localization information. Now if I want to change localization, I would like to change the Action.NAME field so that it displays the proper localized name. I can only get the name, e.g. using item.getAction().NAME but cannot change the field as it is final.

How could I change it's name?


Solution

  • The final field is actually the key for the name property of the action. Change the name property using putValue()

    action.putValue(Action.NAME, newName);