K. Tsang CEng MBCS PMP PMI-ACP OCMJEA OCPJP
K. Tsang CEng MBCS PMP PMI-ACP OCMJEA OCPJP
Stephan van Hulst wrote:Something else: Never write code like this yourself. This is an incredibly persistent and obnoxious pattern. GUI components shouldn't listen to actions they trigger themselves. It's unclear, and it violates all sorts of good coding guidelines.
This is how you should add listeners (starting with Java 8):
For Java 7 and older, do it the way K. Tsang showed you.
Surely you would call that an anti‑pattern.Stephan van Hulst wrote:Something else: Never write code like this yourself. This is an incredibly persistent and obnoxious pattern. . . .
Campbell Ritchie wrote:That is not a brilliant example and it is not something you will want often.
Stephan van Hulst wrote:
A GUI is a GUI. A listener is a listener. They should not be the same class.
Stephan van Hulst wrote:If my class aggregates visual components and exposes methods to access these, I consider it a GUI, so this includes classes that don't extend JFrame or JPanel, but initialize fields that contain references to them.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |