jastrachan | 13 Apr 15:11 2004
Picon

Re: swing example


On 13 Apr 2004, at 13:03, steb wrote:
> Hello,
>   
> I've been trying to make work the code snippet given as a way to call 
> Swing in the article 
> http://viva.sourceforge.net/talk/jug-mar-2004/slides.html.
>  
> I had first to realize that "import all" (import java.awt.*) is broken 
> in beta-4, so we have to import on a class basis, am I right?

I think there have been some issues found & fixed in beta-4 with 
imports. CVS HEAD is working nicely though.

> I don't understand the error generated when the line (1) is not 
> commented out. So where can I get more information about Swing in 
> groovy?
>  
> import java.awt.FlowLayout
>  
> import groovy.swing.SwingBuilder
>  
> sw = new SwingBuilder()
>  
> frame = sw.frame( title:'Counter', size:[200,100]) {
>   panel(layout:new FlowLayout() ) {
>     //display = textField( preferredSize:[200,30], 
> horizontalAlignment:SwingConstants.CENTER ) //(1)
>     button( text:"Inc", size:[65,70], actionPerformed:{ value++; 
> setDisplay() } )
>     button( text:"Clear", size:[65,70], actionPerformed:{ value=0; 
> setDisplay() } )
>     button( text:"Dec", size:[65,70], actionPerformed:{ value--; 
> setDisplay() } )
>   }
> }
>  
> frame.show()
>  
> Error:
> Caught: groovy.lang.MissingMethodException: No such method: panel for 
> class: swing.works$1 with arguments: 
> [[layout:java.awt.FlowLayout[hgap=5,vgap=5,align=center]], 
> swing.works$2@...]
> groovy.lang.MissingMethodException: No such method: panel for class: 
> swing.works$1 with arguments: 
> [[layout:java.awt.FlowLayout[hgap=5,vgap=5,align=center]], 
> swing.works$2@...]
>  at groovy.lang.MetaClass.invokeStaticMethod(MetaClass.java:291)

Using CVS HEAD of Groovy with your script it ran fine (modified version 
below). Though when I uncommented the (1) line above, I got...

macstrac:/workspace/groovy-scratch2/src jstrachan$ groovy Swing.groovy
Caught: java.lang.NullPointerException: Cannot get property: CENTER on 
null object
java.lang.NullPointerException: Cannot get property: CENTER on null 
object

As 'SwingConstants' was not imported and is an undefined object.

Adding the import statement works.

import java.awt.FlowLayout
import javax.swing.*
import groovy.swing.SwingBuilder

value = 1

setDisplay = {
	println "value is now: " + value
}

sw = new SwingBuilder()
frame = sw.frame( title:'Counter', size:[200,100]) {
  panel(layout:new FlowLayout() ) {
  display = textField( preferredSize:[200,30], 
horizontalAlignment:SwingConstants.CENTER ) //(1)
  button( text:"Inc", size:[65,70], actionPerformed:{ value++; 
setDisplay() } )
  button( text:"Clear", size:[65,70], actionPerformed:{ value=0; 
setDisplay() } )
  button( text:"Dec", size:[65,70], actionPerformed:{ value--; 
setDisplay() } )
  }
}

frame.show()

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

Gmane