Using Reflection to Implement MVC

Although Swing has some support for MVC, your model must still understand a lot about Swing. You must either incorporate parts of Swing into your model, or write classes that translate Swing operations into operations for your data model. With a little work, you can write classes that automate some of the repetitive Swing tasks, making it easier to create an MVC-based application.

One of the easiest applications of Reflection for GUI development is in the area of action listeners. When you put a button on the screen, you must create an action listener to handle the button click. To write the listener, you can create an odd-looking anonymous inner class, like this:

 myButton.addActionListener(new ActionListener() ...

Get Special Edition Using Java™ 2 Enterprise Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.