2

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
remi
  • 3,914
  • 1
  • 19
  • 37
  • please why `cbut cannot change the field as it is final.`c , isn't possible to create that as local variable – mKorbel Feb 05 '13 at 10:59
  • 1
    `Action#NAME` is a key used by the `Action` to store a value which other components can use as a display value (by using `Action#getValue(Action.NAME)`). You can change the name by using `putValue` of the action, using `Action.NAME` as the key and the text as the name you want to use. Take a close look at [Using Actions](http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html) – MadProgrammer Feb 05 '13 at 11:02

1 Answers1

10

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);
bowmore
  • 10,842
  • 1
  • 35
  • 43
  • Is it normal that action.getValue(Action.NAME) returns null at first, while the name is not null? The actual name is given by getActionCommand() – remi Feb 05 '13 at 11:12
  • It depends, if the subclass uses the contructor that specifies a name, it should not be null. But a subclass may also override `getValue()` and `putValue()`, so no guarantees there. – bowmore Feb 05 '13 at 11:37