Friday 10 February 2012

SwingBuilder Series: Layouts - BorderLayout



SwingBuilder is the most important class for creating views in Groovy and Griffon, so I think it's worth to learn it deeply. In this entry I'm using the most well known layout in Java desktop development: java.awt.BorderLayout.

To test all your SwingBuilder views SwingPad is a really nice tool for prototyping. SwingPad is bundled with Griffon distributions as a sample app.
BorderLayout can arrange components in 5 different areas: NORTH,SOUTH,EAST,WEST and CENTER.


In this particular case I'm applying this layout to a given panel:
import java.awt.BorderLayout as BL
import java.awt.Dimension

panel(preferredSize:new Dimension(500,200)){
borderLayout()
/* -- NORTH --- */
textField(id:'title',constraints:BL.NORTH)
/* -- CENTER --- */
scrollPane(constraints:BL.CENTER){
table(id:'messages'){
tableModel(){
propertyColumn(header:'language',propertyName:'language')
propertyColumn(header:'text',propertyName:'text')
}
}
}
/* -- SOUTH --- */
button(
id:'executeButton',
constraints:BL.SOUTH,
)
}

And this is how it looks:



In order to apply a BorderLayout to a given container is to declare the node borderLayout() before adding component nodes.

Finally, component arrangement should be made through the constraints property in each of the added components
 button(
id:'executeButton',
constraints:BL.SOUTH,
)

No comments:

Post a Comment