O'Reilly logo

Special Edition Using Java™ 2 Enterprise Edition by Mark Wutka

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required