Building Swing

The elegance of builders is not restricted to XML structure. Groovy provides a builder for creating Swing applications, as well. When working with Swing, we need to perform some mundane tasks such as creating components (like buttons), registering event handlers, and so on. Typically, to implement an event handler we write an anonymous inner class and, in the implementation handler methods, we receive parameters (such as ActionEvent) even if we don’t care for them. SwingBuilder, along with Groovy closures, eliminates the drudgery.

We can use the builder-provided nested or hierarchical structure to create a container (such as JFrame) and its components (such as buttons, text boxes, and so on). Initialize components by using Groovy’s ...

Get Programming Groovy 2 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.