blob: efccab2529415445439fa40166d4b7ff73963d9e [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
//
= Introduction to Groovy
:jbake-type: tutorial
:jbake-tags: tutorials
:jbake-status: published
:icons: font
:syntax: true
:source-highlighter: pygments
:toc: left
:toc-title:
:description: Introduction to Groovy - Apache NetBeans
:keywords: Apache NetBeans, Tutorials, Introduction to Groovy
:reviewed: 2019-01-26
This document gets you started with link:http://groovy.codehaus.org/[+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.
image::images/groovy-newjframe.png[]
[start=3]
. Type *DisplayJFrame* as the Class Name.
[start=4]
. Type *org.demo* as the Package. Click Finish.
image::images/groovy-newjframe2.png[]
When you click Finish the IDE creates the JFrame form and opens the file in the editor.
[start=5]
. Right-click the project node in the Projects window and choose New > Other to open the New File dialog box.
[start=6]
. Select Groovy Class in the Groovy category. Click Next.
image::images/groovy-newgroovyclass.png[]
[start=7]
. Type *GreetingProvider* as the Class Name.
[start=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.
[source,groovy]
----
class GreetingProvider {
def greeting = "Hello from Groovy"
}
----
[start=3]
. Open ``DisplayJFrame.java`` in the editor (if not already open) and click the Design tab.
[start=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.
image::images/groovy-jtextfield.png[]
[start=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.
[source,java]
----
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.
image::images/groovy-codecompletion.png[]
WARNING: JIRA link: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.
[start=6]
. Right-click the project node in the Projects window and choose Run.
When you choose Run, the IDE compiles and launches the application.
image::images/groovy-runproject.png[]
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.