A declarative Swing GUI builder for Groovy applications that provides a concise and maintainable way to create complex Swing user interfaces
—
Comprehensive layout system including all standard Swing layouts plus custom table-based layout manager with enhanced constraint handling.
Enhanced factories for all standard Swing layout managers with simplified attribute handling.
/**
* Generic layout manager factory
*/
class LayoutFactory {
LayoutFactory(Class layoutClass)
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
static void constraintsAttributeDelegate(FactoryBuilderSupport builder, Object node, Map attributes)
}
// Border layout with gap support
borderLayout(hgap: int = 0, vgap: int = 0)
// Components can use constraints: BorderLayout.NORTH, SOUTH, EAST, WEST, CENTER
component(constraints: BorderLayout.NORTH)
// Grid layout with rows and columns
gridLayout(rows: int = 1, cols: int = 0, hgap: int = 0, vgap: int = 0)
// Flow layout with alignment options
flowLayout(alignment: int = FlowLayout.CENTER, hgap: int = 5, vgap: int = 5)
// Card layout for switching between components
cardLayout(hgap: int = 0, vgap: int = 0)
// Overlay layout for layered components
overlayLayout()
// Spring layout for precise positioning
springLayout()Usage Examples:
// Border layout with positioned components
panel {
borderLayout()
label(text: 'Header', constraints: BorderLayout.NORTH)
panel(constraints: BorderLayout.CENTER) {
// main content
}
button(text: 'OK', constraints: BorderLayout.SOUTH)
}
// Grid layout for uniform component sizing
panel {
gridLayout(rows: 2, cols: 3, hgap: 5, vgap: 5)
(1..6).each { i ->
button(text: "Button $i")
}
}
// Flow layout with custom alignment
panel {
flowLayout(alignment: FlowLayout.RIGHT, hgap: 10)
button(text: 'Cancel')
button(text: 'OK')
}Enhanced GridBagLayout support with simplified constraint specification.
/**
* GridBag layout with integrated constraint handling
*/
class GridBagFactory {
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
static void processGridBagConstraintsAttributes(FactoryBuilderSupport builder, Object node, Map attributes)
}
// GridBag layout declaration
gridBagLayout()
// GridBag constraints as separate component or inline attributes
gridBagConstraints(gridx: int = GridBagConstraints.RELATIVE,
gridy: int = GridBagConstraints.RELATIVE,
gridwidth: int = 1, gridheight: int = 1,
weightx: double = 0.0, weighty: double = 0.0,
anchor: int = GridBagConstraints.CENTER,
fill: int = GridBagConstraints.NONE,
insets: Insets = new Insets(0,0,0,0),
ipadx: int = 0, ipady: int = 0)
// Shortcut alias
gbc(gridx: int, gridy: int, weightx: double = 0.0, weighty: double = 0.0)Usage Examples:
// Complex form layout with GridBag
panel {
gridBagLayout()
label(text: 'Name:', gridx: 0, gridy: 0, anchor: GridBagConstraints.EAST)
textField(columns: 20, gridx: 1, gridy: 0, weightx: 1.0, fill: GridBagConstraints.HORIZONTAL)
label(text: 'Email:', gridx: 0, gridy: 1, anchor: GridBagConstraints.EAST)
textField(columns: 20, gridx: 1, gridy: 1, weightx: 1.0, fill: GridBagConstraints.HORIZONTAL)
button(text: 'Submit', gridx: 1, gridy: 2, anchor: GridBagConstraints.EAST)
}
// Using separate constraint objects
panel {
gridBagLayout()
gbc(gridx: 0, gridy: 0, weightx: 0.0)
label(text: 'Label')
gbc(gridx: 1, gridy: 0, weightx: 1.0, fill: GridBagConstraints.HORIZONTAL)
textField(columns: 20)
}Support for BoxLayout and Box container with spacing components.
/**
* Box layout for single-axis arrangement
*/
class BoxLayoutFactory {
BoxLayoutFactory()
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
}
// Box layout with axis specification
boxLayout(axis: int = BoxLayout.X_AXIS) // BoxLayout.X_AXIS, Y_AXIS, LINE_AXIS, PAGE_AXIS
/**
* Box container factory
*/
class BoxFactory {
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
}
// Box containers
box(axis: int = BoxLayout.X_AXIS)
hbox() // Horizontal box
vbox() // Vertical box
// Spacing components
hglue() // Horizontal glue (expandable space)
vglue() // Vertical glue
hstrut(width: int) // Fixed horizontal space
vstrut(height: int) // Fixed vertical space
glue() // General glue component
rigidArea(width: int, height: int) // Fixed size areaUsage Examples:
// Horizontal button layout with spacing
hbox {
button(text: 'First')
hstrut(10) // 10 pixel gap
button(text: 'Second')
hglue() // Push remaining buttons to right
button(text: 'Last')
}
// Vertical layout with flexible spacing
vbox {
label(text: 'Header')
vstrut(5)
panel {
// main content
}
vglue() // Flexible space
hbox {
hglue()
button(text: 'OK')
hstrut(5)
button(text: 'Cancel')
}
}
// Custom box with specific axis
box(axis: BoxLayout.PAGE_AXIS) {
(1..3).each { i ->
button(text: "Item $i")
if (i < 3) rigidArea(width: 0, height: 10)
}
}Custom table-based layout manager for HTML-like table arrangement.
/**
* Custom table-based layout manager
*/
class TableLayout implements LayoutManager2 {
TableLayout()
void addLayoutComponent(Component comp, Object constraints)
void removeLayoutComponent(Component comp)
Dimension preferredLayoutSize(Container parent)
Dimension minimumLayoutSize(Container parent)
void layoutContainer(Container parent)
}
/**
* Table layout factory
*/
class TableLayoutFactory {
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
}
/**
* Table row factory
*/
class TRFactory {
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
}
/**
* Table cell factory
*/
class TDFactory {
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
}
// Table layout structure
tableLayout {
tr { // Table row
td { /* cell content */ } // Table cell
td { /* cell content */ }
}
tr {
td { /* cell content */ }
td { /* cell content */ }
}
}Usage Examples:
// Form layout using table structure
panel {
tableLayout {
tr {
td { label(text: 'First Name:') }
td { textField(columns: 15) }
}
tr {
td { label(text: 'Last Name:') }
td { textField(columns: 15) }
}
tr {
td { label(text: 'Email:') }
td { textField(columns: 20) }
}
tr {
td { /* empty */ }
td {
hbox {
button(text: 'Save')
hstrut(5)
button(text: 'Cancel')
}
}
}
}
}
// Complex layout with mixed content
panel {
tableLayout {
tr {
td {
scrollPane {
tree(id: 'categoryTree')
}
}
td {
splitPane(orientation: JSplitPane.VERTICAL_SPLIT) {
scrollPane { table(id: 'itemsTable') }
scrollPane { textArea(id: 'detailsArea') }
}
}
}
}
}Unified constraint handling system for all layout managers.
/**
* Layout constraint attribute delegate
*/
static void constraintsAttributeDelegate(FactoryBuilderSupport builder, Object node, Map attributes) {
// Handles constraints attribute for all layout managers
}
// Layout-specific constraint examples:
// BorderLayout constraints
component(constraints: BorderLayout.NORTH)
component(constraints: 'North') // String form
// GridBagLayout constraints
component(gridx: 0, gridy: 0, weightx: 1.0, fill: GridBagConstraints.HORIZONTAL)
// CardLayout constraints
component(constraints: 'cardName')
// Custom constraints object
component(constraints: new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL,
new Insets(5, 5, 5, 5), 0, 0))Helper methods and utilities for layout management.
// Component alignment and sizing utilities
component.preferredSize = [width, height]
component.minimumSize = [width, height]
component.maximumSize = [width, height]
// Layout invalidation and revalidation
container.invalidate()
container.validate()
container.revalidate()
// Layout debugging
container.setComponentZOrder(component, index)
component.alignmentX = Component.CENTER_ALIGNMENT
component.alignmentY = Component.CENTER_ALIGNMENTUsage Examples:
// Setting component sizes
button(text: 'Fixed Size Button',
preferredSize: [150, 30],
minimumSize: [100, 25],
maximumSize: [200, 35])
// Dynamic layout updates
def dynamicPanel = panel {
flowLayout()
button(text: 'Add Component', actionPerformed: { e ->
dynamicPanel.add(new JButton("Dynamic ${dynamicPanel.componentCount}"))
dynamicPanel.revalidate()
dynamicPanel.repaint()
})
}
// Alignment in BoxLayout
vbox {
button(text: 'Left', alignmentX: Component.LEFT_ALIGNMENT)
button(text: 'Center', alignmentX: Component.CENTER_ALIGNMENT)
button(text: 'Right', alignmentX: Component.RIGHT_ALIGNMENT)
}Complete list of available layout factories and their purposes.
// Standard Swing layouts
borderLayout() // BorderLayout
cardLayout() // CardLayout
flowLayout() // FlowLayout
gridLayout() // GridLayout
overlayLayout() // OverlayLayout
springLayout() // SpringLayout
// Enhanced layouts
gridBagLayout() // GridBagLayout with constraint support
boxLayout() // BoxLayout with axis specification
// Custom layouts
tableLayout() // HTML-like table layout
// Container factories that affect layout
box() // Box container
hbox() // Horizontal Box
vbox() // Vertical Box
// Spacing components for BoxLayout
glue() // Expandable space
hglue() // Horizontal expandable space
vglue() // Vertical expandable space
hstrut(width) // Fixed horizontal space
vstrut(height) // Fixed vertical space
rigidArea(width, height) // Fixed rectangular spaceInstall with Tessl CLI
npx tessl i tessl/maven-org-codehaus-groovy--groovy-swing