Introduction to Groovy

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.

Contents

Content on this page applies to NetBeans IDE 7.2, 7.3, 7.4 and 8.0

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE 7.2, 7.3, 7.4, 8.0, Java bundle
Java Development Kit (JDK) version 7 or 8
Groovy plugin -

Activate the Groovy Plugin

Support for Groovy is disabled by default when you install the Java version of the IDE. To work with Groovy in the IDE you first need to activate the Groovy plugin in the Plugins manager.

  1. Choose Tools > Plugins from the main menu to open the Plugins manager.
  2. Click the Installed tab in the Plugins manager.
  3. Select Show Details and locate the Groovy and Grails plugin in the list of plugins.

    Alternatively, you can type groovy in the Search field to filter the list of plugins.

  4. Select the checkbox for the Groovy and Grails plugin and click Activate.
    screenshot of Plugins manager
  5. Click Activate in the NetBeans IDE Installer window to activate the Groovy plugin.
  6. Click Finish in the NetBeans IDE Installer window after the installation is complete and then click Close in the Plugins manager.

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.
    screenshot of JFrame Form template in New File wizard
  3. Type DisplayJFrame as the Class Name.
  4. Type org.demo as the Package. Click Finish.
    screenshot of Name and Location panel in New JFrame Form wizard

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

  5. Right-click the project node in the Projects window and choose New > Other to open the New File dialog box.
  6. Select Groovy Class in the Groovy category. Click Next.
    screenshot of Groovy Class template in New File wizard
  7. Type GreetingProvider as the Class Name.
  8. 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"
    
    }
  3. Open DisplayJFrame.java in the editor (if not already open) and click the Design tab.
  4. 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.
    screenshot of JTextfield in DesignJFrame open in the Design view of the editor
  5. 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.

    screenshot of code completion in the editor
  6. Right-click the project node in the Projects window and choose Run.

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

    screenshot of the application

    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.


See Also

NetBeans IDE also supports the Grails web framework, which uses the Groovy language in Java web development. To learn how to use the Grails framework with NetBeans IDE, see Introduction to the Grails Framework.

get support for the NetBeans

Support


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo