Re: Can I use an Action, but have diff text for JButton versus JMenuItem?



Hi Roedy...

Well, I ended up doing as you suggested, creating my own JButton and
JMenuItem subclasses, and adding to my Action, like so (the null check
was required, because the superconstructor calls getText(), and the
instanceof is done in case some dummy decides to use an Action
implementation that is not MyAction):

public MyAction extends AbstractAction {

...

public MyAction(String buttonText, String menuText) {
this.setButtonText(buttonText);
this.setMenuText(menuText);
}

...
}


public MyButton extends JButton {

public MyButton(MyAction action) {
super(action);
}

public String getText() {
Action action = this.getAction();
if ((action == null) || !(action instanceof MyAction)) {
return super.getText();
}

return ((MyAction) action).getButtonText();
}
}

.