concept widget in category groovy

appears as: widgets, widgets, Widget, widget
Groovy in Action, Second Edition

This is an excerpt from Manning's book Groovy in Action, Second Edition.

SwingBuilder cares about proper containment of widgets by following the closure’s nesting structure. Only standalone containers are to be used without a parent. See the Swing API documentation for full coverage of the product classes and their properties.

Listing 11.12. Laying out widgets the common Swing way
import groovy.swing.SwingBuilder import java.awt.BorderLayout as BL swing = new SwingBuilder() frame = swing.frame(title:'Layout Demo') { panel(layout: new BL()) { button(constraints: BL.NORTH, 'North' ) button(constraints: BL.CENTER, 'Center') button(constraints: BL.SOUTH, 'South' ) button(constraints: BL.EAST, 'East' ) button(constraints: BL.WEST, 'West' ) } } frame.pack() frame.visible = true

The second option for laying out widgets is using method calls as listed in table 11.4 that work inside the nesting structure. In addition to Swing’s standard layout options, SwingBuilder also provides simplified access to supporting objects like constraints, glues, and struts. See the Swing API documentation for full coverage of the several layout managers, a description of their “layouting” strategy, their properties together with predefined constant values, and the constraints they rely upon.

Table 11.4. SwingBuilder’s methods for laying out components within a UI

SwingBuilder method

Swing class/method

Notes

borderLayout BorderLayout Layout manager
boxLayout BoxLayout Layout manager; obeys axis, default: X_AXIS
cardLayout CardLayout Layout manager
flowLayout FlowLayout Layout manager
gridBagLayout GridBagLayout Layout manager
gridBagConstraints GridBagConstraints Constraints to be used with GridBagLayout
gbc GridBagConstraints Abbreviation for gridBagConstraints
gridLayout GridLayout Layout manager
overlayLayout OverlayLayout Layout manager
springLayout SpringLayout Layout manager
tableLayout n/a Container; needs nested tr()/td() calls
hbox Box.createHorizontalBox Container
hglue Box.createHorizontalGlue Widget
hstrut Box.createHorizontalStrut Widget; obeys width, default: 6
vbox Box.createVerticalBox Container
vglue Box.createVerticalGlue Widget
vstrut Box.createVerticalStrut Widget; obeys height, default: 6
glue Box.createGlue Widget
rigidArea Box.createRigidArea Widget; obeys size or (width, height), default: 6

This can look like the following code snippet where you first construct the frame and hold a reference to it. When defining the containment structure, you can use this reference at two places: where the frame widget is needed for containment and in the actionPerformed closure. SwingBuilder’s widget method allows placing a predefined widget in the containment structure.