Geertjan is a DZone Zone Leader and has posted 459 posts at DZone. You can read more from them at their website. View Full User Profile

How to Create a Web Service Client with Groovy and NetBeans Platform 6.8

  • submit to reddit

Creating a Simple Prototype

Our simple prototype will consist of an application containing two custom modules, in addition to the 5 mandatory NetBeans Platform modules making up the runtime container, as well as, optionally, any of the other NetBeans Platform modules you will be making use of. The first you created in the previous section: it contains the three Groovy-related JARs. In this section, you create the second module. It will provide a window via the NetBeans API "TopComponent" class, which is a JPanel-like component that provides windows in NetBeans Platform applications. Within the TopComponent, you will have a JLabel, a JTextField, a JButton, and a JTextPane. When the button is clicked, the text in the text field will be sent to the web service... and the response will be displayed in the text pane.

  1. Right-click the ShakespeareSearch "Modules" node in the Projects window and choose "Add New". Give your new NetBeans module a name...

    ...and, in the next step, specify a unique identifier for the new NetBeans module:

  2. Click Finish and you should see that you have two NetBeans modules within your application:

  3. Now we'll let our "ShakesViewer" module provide a window. Right-click the "ShakesViewer" project and choose New | Other | Module Development | Window Component. Click Next. Select "editor" as the "Window Position" and check the "Open on Application Start" checkbox. Click Next. Type "ShakesViewer" in "Class Name Prefix" and then click Finish.

  4. Now use the Palette in the IDE to drag and drop a JLabel, JTextField ("searchField"), JButton ("findButton"), and JTextPane ("resultPane") onto the TopComponent in "Design" mode. Rearrange the components until you have designed a window that looks like this:

  5. Right-click the application and choose Run. The application starts up, installing all the NetBeans Platform modules that have been included (to see which these are, right-click the application node in the Projects window, choose Properties, and look in the Libraries tab), together with the two custom modules you created thus far. Don't continue with the rest of this article unless you see this:

  6. Before our second module can use the Java code provided by the first module, we need to set a dependency on the first module from the second module. I.e., not only must the first module make packages available to the whole application, but any application ALSO has to set a dependency on that first module. That means, we have a bi-directional contract between the modules in our application.

    Right-click the ShakesViewer module, choose Properties, and click "Add Dependency" in the Libraries tab. Then find the first module's unique ID and click OK:

  7. To enable us to integrate with the NetBeans Platform's Progress bar (thus handling the scenario where we don't want the UI to be blocked while interaction is done with the web service), set another dependency, this time on the "Progress API":

    Now we're ready to start adding some code!

  8. In the IDE, double-click the "findButton" so that the source editor opens, with the cursor in the "findButtonActionPerformed" method. There, add this very simple piece of code, for retrieving a quotation from the web service and displaying it in the text pane:
    private void findButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    Thread t = new Thread(new Runnable() {
    public void run() {
    //Use the NetBeans Progress API to define a new progress handle:
    ProgressHandle p = ProgressHandleFactory.createHandle(
    "Fetching the Shakespeare quote for "
    + "'" + searchField.getText() + "'...");
    //Start the progress bar:
    //Find a new Shakespeare quote via the Groovy-based client:
    ShakesWsClient client = new ShakesWsClient();
    //Populate the UI components with the current object's values:
    //Populate the status bar with the Shakespeare play returned:
    StatusDisplayer.getDefault().setStatusText("Play: "
    + client.getPlay());
    //Finish the progress bar:
    //Note: The progress bar takes 3 lines to include,
    //and helps us with all UI-blocking activities!

    Note: Read more about the NetBeans Progress API here.

  9. Run the application and then type a search string, such as "love is like" in the text field: 

    Click the Find button. Notice that the UI is still usable, i.e., not blocked, and that the progress bar gives feedback to the user about the processing that's taking place. Once the quotation is retrieved, it appears in the text pane, as shown below:

Congratulations, your simple prototype is working as expected. Now let's include a lot of the interesting & powerful NetBeans API classes, to display the retrieved results in a JTree!

Published at DZone with permission of its author, Geertjan Wielenga.


Cej Hah replied on Mon, 2009/12/14 - 2:40pm

Geeez, it's almost 2010, can't we get away from Big Web Services already?!  REST is your friend.  SOAP is your enemy.

Ning Sun replied on Wed, 2009/12/16 - 3:49am

Really nice article.

I have a question about the code when binding mouse listener to bean tree view. That is step 8 in "Using NetBeans Platform Swing Components", line 15. How you got the reference of resultPane in MyBTV? Is there any trick?

Thank u for your great article.



Sven Alfred replied on Thu, 2009/12/31 - 3:38pm

Great walkthrough. SOAP or not... doesnt matter.

Combining the power of the platform with the simplicity of Groovy. I will use this as recipe for my own platform experiments.



Jeff Schwartz replied on Fri, 2010/01/29 - 9:16am


Very groovy to see these examples in Groovy. IMO Groovy is the gem of the Java virtual machine. I find myself using it a lot. I fall back to Java when I can find a legitimate reason to, such as when addressing critical performance issues. Otherwise, Groovy just makes more sense to me.

A lot of the developers I speak with believe Oracle's purchase of Sun doesn't bode well for Netbeans. Nothing that I have read leads me to believe that Oracle intends to throw their 'official' support behind it. Maybe now is time to set Netbeans free from any single corporate sponsor and before Oracle kills it with neglect.

 Be well.

 Jeff @

Mateo Gomez replied on Tue, 2012/04/17 - 12:30am

 You just made my life easier


 mexican dessert recipes

Matt Coleman replied on Tue, 2012/04/17 - 12:43am


 this makes want to get to know netbeans...great job you guys

graphic design buffalo

Eugenios Ampelius replied on Mon, 2013/09/30 - 10:49am

 Groovy does a great job. What's the deal with REST, though? I haven't used it. Is it really better than SOAP? Thank you.

Cata Nic replied on Mon, 2013/09/02 - 4:10am

 The platform can be extended to something custom and dedicated do the client's needs ?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.