The node inside the SwingBuilder is gridBagLayout(). In my opinion if you wanted to do something complicated you'd better use MIGlayout, everything else can be arrange with BorderLayout,GridLayout, and BoxLayout.
This example looks like this:
Basically for each component you have to think as if you were ordering components inside a x,y grid using grids,gridy respectivelly. Then you can tell the component to be resized when the layout is resized (fill:HORIZONTAL).
import static java.awt.GridBagConstraints.*
import javax.swing.*
import java.awt.*
def defaultInsets = [0,0,10,0]
panel(border:BorderFactory.createEmptyBorder(10,10,10,10)){
gridBagLayout()
label(
text:"Name",
constraints: gbc(gridx:0,gridy:0,fill:HORIZONTAL,insets:defaultInsets)
)
textField(
preferredSize:new Dimension(300,20),
constraints:gbc(gridx:1,gridy:0,gridwidth:REMAINDER,fill:HORIZONTAL,insets:defaultInsets)
)
label(
text:"Address",
constraints:gbc(gridx:0,gridy:1,fill:HORIZONTAL,insets:defaultInsets)
)
comboBox(
items:["Street","Avenue"],
preferredSize: new Dimension(100,20),
constraints:gbc(gridx:1,gridy:1,fill:HORIZONTAL,insets:defaultInsets)
)
textField(
constraints:gbc(gridx:3,gridy:1,gridwidth:REMAINDER, fill:HORIZONTAL,insets:defaultInsets)
)
label(
text:"Age",
constraints:gbc(gridx:0,gridy:2,fill:HORIZONTAL,insets:defaultInsets)
)
spinner(
preferredSize: new Dimension(50,20),
constraints:gbc(gridx:1,gridy:2,anchor:WEST,insets:defaultInsets)
)
button(
text:'Save Record',
constraints:gbc(gridx:0,gridy:3,insets:[10,0,0,0],gridwidth:3,anchor:WEST)
)
}
I also want components to have a margin, that's why I set the insets property. I'm using the anchor property to tell the layout that I want the age spinner aligned to the left. Finally I want the save button to span all columns but to be align to the left. So:
- gridx,gridy to arrange components
- fill: to tell the component how to resize itself
- insets: margins between components
- anchor: to align the component within the grid area
Very Old post, perhaps you will never see it. But, I give a shot.
ReplyDeleteI tried this(2016) and this isn't work. I use 2.4.7 Groovy version and I gain a StackOverflow Expection, so I tried since 1.8 and the same error. If you are here.
My emal is: andremb1@gmail.com