Asaf
Asaf

Reputation: 189

Java Swing: Using ActionMap

I've seen a post earlier that recommends favoring Actions over listeners. I am not so familiar with the ActionMap. What are the disadvantages of the each approach? I like to make inner classes that implement listeners. Is that a bad practice?

Thanks

Upvotes: 12

Views: 7134

Answers (1)

trashgod
trashgod

Reputation: 205785

Key bindings rely on actions. There is a potential risk that Action "implementations tend to be more expensive in terms of storage than a typical ActionListener." In practice, the problem does not arise. Here is an example.

Using KeyListener is conceptually easier to understand but typically more difficult to maintain. One inevitably ends up re-creating some kind of binding machinery. This example, while instructive and even appropriate for a game, is needlessly complex.

Upvotes: 6

Related Questions