Groovy Scripting / Groovy GUI

The Groovy documentation does not include much on building a GUI using SwingBuilder a better source is github.com/apache/groovy-examples

If you are using a Java control panel and want to be able to access the settings globally drop the 'ControlPanel' in

panel = new ControlPanel() {
//methods
//control elements
 {
 //add controls
 //monitors
 //tooltips etc.
 }
}
checkboxSet = panel.checkbox.isSelected();

Groovy has many features that lets you write more condensed code, making it easier to read and maintain. Take an action listener in bash:

aButton.addActionListener(new ActionListener() {
	public static void actionPerformed(ActionEvent ae) {
		// callback code here
	}
});

may preferrably be rewritten as:

aButton.addActionListener(ae -> { callback here });

If you miss the explicit types, then you can add them, like this:

aButton.addActionListener((ActionEvent ae) -> { callback here });

Finally, if the lambda expression is a single statement, you can do away with the { } too.


To see a demo of a Groovy GUI, based on the swing example from the above link, copy the code below and paste it into the System console, choose Groovy and then evaluate. It may take a few seconds for the GUI to appear. Use this code as

/*
Copyright 2020 RobM. 

Code modified from  https://github.com/apache/groovy-examples/blob/3bd1e1811c5306b0d8dbf65c9bde6091596a1307/src/main/groovy/swing/Widgets.groovy 
to run in jAlbum's system console

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. 
*/

import java.awt.Color
import javax.swing.SwingConstants
import javax.swing.WindowConstants
import groovy.swing.SwingBuilder

def swing = new SwingBuilder()

def showUnownedDialog(event) {
	unownedDialog.show();
}

def showOwnedDialog(event) {
	ownedDialog.show();
}

	unownedDialog = swing.dialog(
		title:'unrooted dialog',
		location: [200, 200],
		pack:true,
		defaultCloseOperation:WindowConstants.DISPOSE_ON_CLOSE
		) {
			label("I am unowned, but not unwanted");
		}

	def frame = swing.frame(
		title:'FrameTitle',
		location:[100,100],
		size:[800,400],
		defaultCloseOperation:WindowConstants.DISPOSE_ON_CLOSE) {

		menuBar {
			menu(text:'File') {
				menuItem() {
					action(name:'New', closure:{ println("clicked on the new menu item!") })
				}
				menuItem() {
					action(name:'Open', closure:{ println("clicked on the open menu item!") })
				}
				separator()
				menuItem() {
					action(name:'Save', enabled:false, closure:{ println("clicked on the Save menu item!") })
				}
			}
			menu(text:'Dialogs') {
				menuItem() {
					action(name:'Owned Dialog', closure: this.&showOwnedDialog)
				}
				menuItem() {
					action(name:'Unowned Dialog', closure: this.&showUnownedDialog)
				}
				def deeplyOwnedDialog = swing.dialog(
					title:'rooted dialog #2',
					location: [200, 200],
					pack:true,
					defaultCloseOperation:WindowConstants.DISPOSE_ON_CLOSE
					) {
					label("ownership is deep");
				}
				menuItem() {
					action(name:'Deeply Owned Dialog', closure: {deeplyOwnedDialog.show()} )
				}
			}
		}

		tabbedPane() {

			//colorChooser(
			//    name:"Color Chooser",
			//    color: 0xfeed42)

			panel(name:"Formatted Text Fields") {
				gridLayout(columns: 2, rows: 0)
				label("Simple Constructor:")
				formattedTextField()
				label("Date Value")
				formattedTextField(value: new java.util.Date())
				label("Integer Value")
				formattedTextField(value: new java.lang.Integer(42))
				label("Date Format")
				formattedTextField(format: java.text.DateFormat.getDateInstance())
				label("Currency Format ")
				formattedTextField(format: new java.text.DecimalFormat("¤###.00;(¤###.00)"))
			}

			panel(name:"Sliders") {
				flowLayout()
				slider(minimum:-100, 
					maximum:100, 
					majorTickSpacing: 50,
					orientation: SwingConstants.VERTICAL, 
					paintLabels:true)
				slider(minimum:-100, 
					maximum:100, 
					orientation: SwingConstants.VERTICAL, 
					paintLabels:true,
					paintTicks:true,
					majorTickSpacing: 50,
					minorTickSpacing: 10,
					snapToTicks:true,
					paintTrack:true)
			}

			panel(name:"Spinners") {
				gridBagLayout()
				label(
					text:"Tempuature in London:",
					insets:[12, 12, 2, 2],
					anchor: EAST,
					gridx: 0)
				spinner(
					model:spinnerNumberModel(minimum:-10, 
						maximum: 40, 
						value:20,
						stepSize:5),
					insets:[12, 3, 2, 12],
					anchor: WEST,
					gridx: 1,
					fill: HORIZONTAL)
				label(
					text:"Baseball Leagues:",
					insets:[3, 12, 2, 2],
					anchor: EAST,
					gridx: 0)
				spinner(
					model:spinnerListModel(
						list: ["Major League", "AAA", "AA", "A", "Rookie", "Semi-Pro", "Rec A", "Rec B"],
						value: "AA"),
					insets:[3, 3, 2, 12],
					anchor: WEST,
					gridx: 1,
					fill: HORIZONTAL)
				label(
					text:"Today's Date:",
					insets:[3, 12, 2, 2],
					anchor: EAST,
					gridx: 0)
				spinner(
					model:spinnerDateModel(calendarField: Calendar.HOUR_OF_DAY),
					insets:[3, 3, 2, 12],
					anchor: WEST,
					gridx: 1,
					fill: HORIZONTAL)
			}

			panel(name:"Border Layout") {
				borderLayout()
				label(text:"Border Layout", 
					  constraints:NORTH,
					  horizontalAlignment:SwingConstants.CENTER)
				label(text:"South", 
					  constraints:SOUTH,
					  background:Color.YELLOW,
					  opaque:true,
					  horizontalAlignment:SwingConstants.CENTER,
					  toolTipText:"Tooltip on south")
				label(text:"West", 
					  constraints:WEST,
					  background:Color.ORANGE,
					  opaque:true,
					  horizontalAlignment:SwingConstants.CENTER,
					  toolTipText:"Tooltip on west")
				label(text:"East", 
					  constraints:EAST,
					  background:Color.GREEN,
					  opaque:true,
					  horizontalAlignment:SwingConstants.CENTER,
					  toolTipText:"Tooltip on east")
				label(text:"Center", 
					  constraints:CENTER,
					  background:Color.WHITE,
					  opaque:true,
					  horizontalAlignment:SwingConstants.CENTER,
					  toolTipText:"<html>This is not the tooltip you are looking for.<br><i>*waves hand*</i>")
			}
		}

		ownedDialog = swing.dialog(
			title:'rooted dialog',
			location: [200, 200],
			pack:true,
			defaultCloseOperation:WindowConstants.DISPOSE_ON_CLOSE
			) {
			label("j00 h4v3 b33n 0wn3xed");
		}
	}        
	frame.show()

Below is some code examples, found on the internet, to help you get started. From sourceforge.net/p/freeplane

import groovy.swing.SwingBuilder
import java.awt.FlowLayout as FL
import javax.swing.BoxLayout as BXL
import javax.swing.JFrame

def s = new SwingBuilder()
s.setVariable('myDialog-properties',[:])
def vars = s.variables
def dial = s.dialog(title:'Dialog 1', id:'myDialog', modal:true, locationRelativeTo:window, owner:window, defaultCloseOperation:JFrame.DISPOSE_ON_CLOSE, pack:true, show:true) {
    panel() {
        boxLayout(axis:BXL.Y_AXIS)
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            checkBox(id:'check', text:'Checkbox')    // append 'selected: true' to have the checkbox already selected
        }
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            label('Text')
            textArea(id:'textArea', columns:10, rows:2)
        }
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            label('Completion:')
            buttonGroup().with { group ->  
                radioButton(id: '0', text: '0%', selected: true, buttonGroup: group)  
                radioButton(id: '25', text: '25%', buttonGroup: group)  
                radioButton(id: '50', text: '50%', buttonGroup: group)  
                radioButton(id: '75', text: '75%', buttonGroup: group)  
                radioButton(id: '100', text: '100%', buttonGroup: group)  
            }  
        }
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            label('Combo')
            comboBox(id:'combo', items:['Option 1', 'Option 2'])
        }
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            button('Submit', preferredSize:[80, 24],
                   actionPerformed:{
                       vars.dialogResult = 'ok'
                       dispose()
            })
            button('Cancel', preferredSize:[80, 24],
                   actionPerformed:{
                       vars.dialogResult = 'cancel'
                       dispose()
            })
        }
    }
}

if (vars.dialogResult == 'ok') {
    def child = node.createChild(vars.textArea.text)
    def percent = ["0", "25", "50", "75", "100"].find{ vars[it].selected }
    child.icons.add(percent + "%")
    child["combo"] = vars.combo.selectedItem
    child["checkbox"] = (vars.check.selected ? 'checked' : 'not checked')
}

From josh-in-antarctica.blogspot.com A nicer way to do it using Groovy's with keyword: from

buttonGroup().with {
    add radioButton(text: 'Option 1')
    add radioButton(text: 'Option 2')
} 

Spinners take the form of

 spinner (id:'spinnerName', model:spinnerNumberModel(minimum:0, maximum: 40,  value:20,  stepSize:5))
 spinner(id:'spinnerName', model:spinnerListModel( list: ["Text one", "two", "three", "four", "five"], value: "three")
 spinner(id:'spinnerName', model:spinnerDateModel(calendarField: Calendar.HOUR_OF_DAY)