Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Jim_Ross

[java] OO Styles

This topic is 6948 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Try this instead:

public static final String OK_COMMAND = "OK";
...
JButton jb = new JButton(getLocalizedCommand(OK_COMMAND));
jb.addActionListener(this);
jb.setActionCommand(OK_COMMAND);
...
public void actionPerformed(ActionEvent evt)
{
if (evt.getActionCommand().equals(OK_COMMAND))
{
...
}
}

You''d write the "getLocalizedCommand" method would get the localized String for the button label, based on the action command OK_COMMAND. This allows for localization, and you can also add the action command to other controls, if need be. It also allows you to use a button with an icon instead of text, if you wanted to. Basically, the idea is to remove the functionality from the presentation of the functionality. That way you can change the label without changing anything else (which is why Action classes are so cool, but that''s another story).

Still, for simple, one-shot things like dialog buttons, an anonymous inner class is sometimes the better way to go, because that way you don''t have to do ANY testing to see what control generated the event.

Share this post


Link to post
Share on other sites
Advertisement
Hey, but doesn''t e.getActionCommand(); do that string comparison thing or am I missing the trick?

Share this post


Link to post
Share on other sites
Sorry, but I had error in my code, because the event which makes my custom button click in mouse released section got accidentally deleted when I made some arrangements, but still, doesn''t that e.getActionCommand() thing work with awt buttons when eventing them by label?

Time comes, time goes and I only am.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!