Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

Groovy is supported natively in NetBeans IDE from NetBeans IDE 6.5 onwards.

How To Get Started

A Groovy editor is a standard part of the "Java" distribution, i.e., the distribution aimed at Web development. Also included in that distribution is support for Grails.In this section, we create a Java application.Click here to download the "Java" distribution, 6.5 or above:

Creating a First Application

In this section, we create a Java application.

  1. Choose File > New Project (Ctrl-Shift-N) and then select "Java Application" from the "Java" category.

Click Next.

  1. In Project Name, type "GroovyJavaDemo"; in Project Location, select the folder where the application will be created.

Make sure to unselect the Create Main Class checkbox. Click Finish.

  1. Right-click the application and choose Properties. In the Project Properties dialog box, select the Groovy panel and select the Enable Groovy checkbox:

Click OK.

You now have a Java application that includes support for Groovy. The Groovy JAR is on the application classpath and the Ant script build.xml has been extended with new scripts that will enable the Groovy and Java code to be compiled together.

Creating a Groovy Class

In this section, we create a JFrame and a Groovy class.

  1. Right-click the project and choose New | Other. In the New File dialog, choose Swing GUI Forms | JFrame Form. In Class Name, type "DisplayJFrame"; in Package, type "org.demo":

Click Finish. The JFrame is created.

  1. Open the New File dialog again and choose Groovy | Groovy Class:

Click Next.

  1. In Class Name, type GreetingProvider; in Package, select the package you created previously:

Click Finish. The Groovy file is created. Your project structure should now be as follows:

Calling Groovy from Java

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

  1. In the Groovy class, define a greeting variable within the class definition, as shown below:class GreetingProvider
    Unknown macro: { def greeting = "Hello from Groovy" }
  2. Add a JTextField to the JFrame. In the Source view of the JFrame, initialize the Groovy class and call its getGreeting() method, as shown below:GreetingProvider provider = new GreetingProvider();

public DisplayJFrame() {
String greeting = provider.getGreeting().toString();
}Notice that you can use code completion in the Java class to find the methods you need in the Groovy class:

  1. Run the application and the text from the Groovy class is shown in the JFrame:
    You now know how to use NetBeans IDE to create a basic Java application that interacts with Groovy.
  • No labels