jastrachan | 13 Apr 16:15 2004
Picon

Re: Using BoxLayout


On 13 Apr 2004, at 13:41, Kevin Jones wrote:
> BoxLayout has no default constructor and one of its parameters is a 
> 'target'
> which is a reference to the class component that hosts the layout. 
> Using a
> SwingBuilder something like this
>
>
> frame = swing.frame(title:'This is a Frame', location:[100,100],
> size:[800,400]){
> 	panel(layout:new BoxLayout(frame.contentPane, BoxLayout.X_AXIS)) {
>       	textField(text:'Name');
> 	}
> }
>
> doesn't work. I get this
>
> Caught: java.lang.NullPointerException: Cannot get property: 
> contentPane on
> null object

This is because the swing.frame() method hasn't returned yet. Indeed 
there's a chicken-egg issue with this as we can't create the layout 
until we have the component. One workaround would be

p = panel() {
p.layout = new BoxLayout(p, BoxLayout.X_AXIS)

> which implies that when frame.contentPane is used frame is still null. 
> Is
> there a 'groovy' way of achieving what I want?

I added an extra helper method called boxLayout() which can be used to 
create box layouts. Its available in CVS HEAD if you don't mind using 
that.
Note that this must be specified inside the component on which its 
setting the layout. e.g.

import javax.swing.BoxLayout
import groovy.swing.SwingBuilder
 
sw = new SwingBuilder()
 
frame = sw.frame( title:'Demo', size:[200,100]) {
	panel() {
		boxLayout(axis:BoxLayout.X_AXIS)
	  	textField(text:'Name')
	}
}
 
frame.show()

James
-------
http://radio.weblogs.com/0112098/

Gmane