The Groovy Swing Console allows a user to enter and run Groovy scripts. This page documents the features of this user interface.


The Groovy Console:

  1. Groovy Console is launched via "groovyConsole" or "groovyConsole.bat", both located in "$GROOVY_HOME/bin"
  2. The Console has an input area and an output area.
  3. You type a Groovy script in the input area.
  4. When you select "Run" from the "Actions" menu, the console compiles the script and runs it.
  5. Anything that would normally be printed on System.out is printed in the output area.
  6. If the script returns a non-null result, that result is printed.


Running Scripts

Handy tips for running scripts:

Editing Files

You can open any text file, edit it, run it (as a Groovy Script) and then save it again when you are finished.

History and results

And more

Embedding the Console

To embed a Swing console in your application, simply create the Console object,
load some variables, and then launch it. The console can be embedded in either Java or Groovy code.
The Java code for this is:

import groovy.ui.Console;


    Console console = new Console();
    console.setVariable("var1", getValueOfVar1());
    console.setVariable("var2", getValueOfVar2());;


Once the console is launched, you can use the variable values in Groovy code.

An example of how to embed either the GroovyConsole or GroovyShell in a Spring Web application can be found at Embedding a Groovy Console in a Java Server Application

Visualizing script output results

 You can customize the way script output results are visualized. Let's see how we can customize this. For example, viewing a map result would show something like this:

 What you see here is the usual textual representation of a Map. But, what if we enabled custom visualization of certain results? The Swing console allows you to do just that. First of all, you have to ensure that the visualization option is ticked: View -> Visualize Script Results — for the record, all settings of the Groovy Console are stored and remembered thanks to the Preference API. There are a few result visualizations built-in: if the script returns a java.awt.Image, a javax.swing.Icon, or a java.awt.Component with no parent, the object is displayed instead of its toString() representation. Otherwise, everything else is still just represented as text. Now, create the following Groovy script in ~/.groovy/OutputTransforms.groovy:

import javax.swing.*

transforms << { result ->
    if (result instanceof Map) {
        def table = new JTable(
            result.collect{ k, v ->
                [k, v?.inspect()] as Object[]
            } as Object[][],
            ['Key', 'Value'] as Object[])
        table.preferredViewportSize = table.preferredSize
        return new JScrollPane(table)

The Groovy Swing console will execute that script on startup, injecting a transforms list in the binding of the script, so that you can add your own script results representations. In our case, we transform the Map into a nice-looking Swing JTable. And we're now able to visualize maps in a friendly and attractive fashion, as the screenshot below shows:


AST browser

Groovy Console can visualize the AST (Abstract Syntax Tree) representing the currently edited script, as shown by the screenshot below. This is particularly handy when you want to develop AST transformations.