The code taken from the book by Bashar Abdul-Javad, according to the author, should create a GUI with a text field, a button and a menu with one item.

And after entering the text and pressing the button, the text is displayed in the widget.

When I try to execute in groovyConsole.exe (Ctrl + R) - it works ...

When I run ###. Groovy, no - why?

import groovy.swing.SwingBuilder import javax.swing.JOptionPane swing = new SwingBuilder() showText = swing.action(name: 'Show Text', closure: { JOptionPane.showMessageDialog(frame, message.text)}) frame = swing.frame(title: 'Action Demo'){ menuBar{ menu('Tools'){ menuItem('Show text', action: showText) } } panel(){ message = textField(columns: 10) widget(button) } } button = swing.button(text: 'Show text', action: showText) frame.pack() frame.show() 
  • When I run ###. Groovy, no - explain how you do it? - Mikhail Vaysman
  • Now the next question - issue it with a separate question. if necessary, keep the context - add a link to this. - Nofate

1 answer 1

Try to put this line here.

 button = swing.button(text: 'Show text', action: showText) 

Before

 frame = swing.frame(title: 'Action Demo'){ 

Turn out like this

 import groovy.swing.SwingBuilder import javax.swing.* swing = new SwingBuilder() showText = swing.action(name: 'Show Text', closure: { JOptionPane.showMessageDialog(frame, message.text) }) button = swing.button(text: 'Show text', action: showText) frame = swing.frame(title: 'Action Demo') { menuBar { menu('Tools') { menuItem('Show text', action: showText) } } panel() { message = textField(columns: 10) widget(button) } } frame.pack() frame.show() 

Most likely this will fix your problem.