Creating a RCP Application in Netbeans – [Part 2]

Adding a Module with Functionality.

In our last tutorial we made a pretty bare bones application with nothing in it.

We made two modules –

  • a branding module that offers a splash screen
  • and an application module that acts as an entry point to the application (like a main() method if you want to see it that way)

Now we want to add modules with some proper functionality.

  1. Add a new Maven module by selecting ‘Create New Module..’ via a right click on the modules folder. Call it something like CustomerModule.

image001

  1. By default the Project name is the same as the Maven Artifact Id. Change it to something like ‘HappyDog CustomerModule’.

image002

Now we should see something like this :

image003

  1. Let’s create some packages in our CustomerModule and add three java files :

image004

The java files represent standard patterns you might find in a Swing application; the MVC pattern  and delegate pattern.

  • The Customer is a simple POJO :

  • The CustomerTableModel is a Swing table model class which uses the Customer POJO to display data in a table.

  • Finally we have a delegate which centralizes calls associated with the customer module.

 

The delegate is a thread safe singleton that contains some incomplete methods that we may want to implement later.

 

 

Creating the GUI

Create a Panel

  1. Now right click the happydog.customermodule.ui package and select New àWindow.
  2. Make sure the ‘Open on Application Start’ checkbox is set.

image005

  1. Select ‘Next’.
  2. Add a ClassName prefix called ‘Customer’

image006

  1. Select ‘Finish’.

The New Window wizard has created a java file ending in TopComponent and a form file also ending in TopComponent.

TopComponent is the basic unit of display in NetBeans and represents a window, tabbed pane or inner frame. A lot of the code in the java file is auto-generated and non-editable. It may look a bit scary but it is really simple if understand that there are only a few points where you can modify the code. The designer handles most of the wiring.

The CustomerTopComponent.form file is a file that NetBeans uses internally to store information about the GUI component. Generally this file is kept as invisible as possible to the end user.

 

Add GUI Components

If we open the CustomerTopComponent file and select design mode we will see a palette where we can drag components onto a panel :

image007


  1. Drag a table and three buttons onto the panel as shown in the picture.
  2. Select the jTable, and then switch the ‘properties’ view to the ‘code’ view. Change the name to something more readable such as CustomerTable :

image008

Hookup Events

When we fire up the screen we want to load up all our customers in the system, ie we want to populate the table with all the data from our CustomerDelegate.getCustomers() call.

  1. Open the CustomerTopComponent file and change the constructor by adding this code :

 

We can change the text on our buttons too by changing the text property on the buttons. Make sure you also rename the variable name of the buttons as well (via the Code tab).

  1. Change the button text via the properties tab :

image009

The event hooks can be found in the Events tab next to the Code tab. Just select one of the event methods you want to implements (eg. actionPerformed).

  1. Select the actionPerformed event :

image010

 

For now we can put some dummy code in the empty methods.

 

 

Incorporating Module Into Super Project

If we do a ‘clean build’ on the project and then run it, we should see :

 

image011

 

 

Ignore the start screen and other tabs that appear. The module is not supposed to run by itself but sometimes it’s good to get a quick look at how it might look.

As we can see it all looks pretty correct. The ‘TopComponent’ is being shown on startup with the table data.

 

At the end of the day we really want to run this module within our super project HappyDog. So we should add the module we created as a dependency in the application module.

  1. Right click HappyDog app à dependency.
  2. Select ‘Add Dependency..’
  3. Under ‘Open Projects’ select the Customer Module.

image012

Your folder structure should look like this :

image013

 

Do a Maven clean and build on the super project, followed by a clean and build on the ‘HappyDog app’ project.

Right click on the HappyDog app, select ‘Run’ and you should see the module has magically appeared in the app project.

 

image014

 

 

Part 3 Coming Soon!!!

All the code for this tutorial can be found on github.

 

 


Posted in Netbeans RCP, Netbeans Tutorial, Tutorial | Tagged , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

THREE_COLUMN_PAGE