Introduction to Groovy

Last reviewed on 2019-01-26

This document gets you started with Groovy in NetBeans IDE. You will create a Java application, add a JFrame, and retrieve a simple message from a Groovy file.

Creating the Application

In this section you will create a new Java application.

  1. Choose File > New Project (Ctrl-Shift-N;⌘-Shift-N on Mac) from the main menu to open the New Project wizard.

  2. Select Java Application in the Java category. Click Next.

  3. Type GroovyJavaDemo as the Project Name and specify the folder where you want to create the application in the Project Location field.

  4. Deselect the Create Main Class checkbox. Click Finish.

When you click Finish the IDE creates the project and displays a project node in the Projects window.

Creating the Java Class and the Groovy File

In this section you will create a JFrame and a Groovy class.

  1. Right-click the project node in the Projects window and choose New > Other to open the New File dialog box.

  2. Select JFrame Form in the Swing GUI Forms category. Click Next.

groovy newjframe
  1. Type DisplayJFrame as the Class Name.

  2. Type org.demo as the Package. Click Finish.

groovy newjframe2

When you click Finish the IDE creates the JFrame form and opens the file in the editor.

  1. Right-click the project node in the Projects window and choose New > Other to open the New File dialog box.

  2. Select Groovy Class in the Groovy category. Click Next.

groovy newgroovyclass
  1. Type GreetingProvider as the Class Name.

  2. Select the org.demo package from the Package dropdown list. Click Finish.

When you click Finish the IDE creates the Groovy file and opens the file in the editor.

If you expand the project node in the Projects window you can see that the two files that you created are under the Source Packages node.

Calling Groovy from Java

In this section, you will code the interaction between the Groovy file and the Java class.

  1. Open GreetingProvider.groovy in the editor (if not already open).

  2. Define a greeting variable within the class definition by adding the following code (in bold). Save your changes.

class GreetingProvider {

    def greeting = "Hello from Groovy"

}
  1. Open DisplayJFrame.java in the editor (if not already open) and click the Design tab.

  2. Open the Palette (Window > Palette from the main menu) and drag and drop a Text Field element ( jTextField ) from the Palette and into the JFrame.

groovy jtextfield
  1. Click the Source tab in the editor and add the following code at the top of the class body to instantiate the Groovy class and in the constructor to call the getGreeting() method in the Groovy class.

public class DisplayJFrameForm extends javax.swing.JFrame {

    GreetingProvider provider = new GreetingProvider();

    public DisplayJFrame() {
        initComponents();
        String greeting = provider.getGreeting().toString();
        jTextField1.setText(greeting);
    }

You can use code completion in the Java class to find the methods you need in the Groovy class.

groovy codecompletion
JIRA https://issues.apache.org/jira/browse/NETBEANS-1996 has to be solved for the Java editor to recognize the Groovy reference and for code completion shown above to work. Despite that, and even though the Java editor shows the Groovy reference with a red underline, you can still follow the step below and successfully run the application.
  1. Right-click the project node in the Projects window and choose Run.

When you choose Run, the IDE compiles and launches the application.

groovy runproject

In the window of the application you can see that the text from the Groovy class is displayed in the text field.

You now know how to create a basic Java application that interacts with Groovy.