Simple Login Dialog in Java Swing

Here is a simple login dialog for anyone to use for free. It looks like this :


Creating a login dialog is not rocket science, however I come across login dialogs all the time. It’s just nice to not have to “roll-your-own” so to speak, so I thought I’d share this bit of code for anyone that it could help.

It basically uses BorderLayout as a base, a FlowLayout aligned to the right for the two buttons, and the textfields and labels use a GridBagLayout for correct placement in the center panel. I’ve added some event listeners to the code for convenience.

I will add it to my Swing Library github project soon :

Here is the code :

Posted in Swing, Swing Components | Tagged , | Leave a comment

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.


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


Now we should see something like this :


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


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.


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


  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 :


  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 :


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 :


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 :



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 :





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.


Your folder structure should look like this :



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.





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

Row Number Column in JTable

Ever wanted to have a number column in your JTable similar to Excel?

This utility class I made takes in a JTable and appends a number column.

Let’s take a look at it!

The rendering of the number column matches the rendering of the headers :


As you scroll horizontally, the row column remains fixed in place :


Highlighting of rows also incorporates the number column :




As the numbers increase, the width of the column needs to be changed. This happens automatically and also shrinks when the numbers get smaller :


How does this work?

The row column is really just a JTable with a single column. This table is then lined up next to the main table making sure that all metrics match, so that it appears to just be another column in the JTable. A simple table model is required which is just an numerical incrementation, and listeners need to be set up to link the two tables. When the vertical scrollbar is moved, the numerical table listens and changes its position respectively. When a horiyontal scroll happens then the numerical table ignores it.

All the code can be found on my github repository

Or you can just copy and paste the code below :


Posted in Swing, Swing Components | Tagged , , | 1 Comment

Javascript Framework Review

Recently I had to do a Javascript Framework evaluation for a client.

Prerequisites for the frameworks was that the framework should have sophisticated tables (aka Grids) and sophisticated dialogs. Also of interest in our evaluation was if the framework could do treetables.

The things we were interested in for a grid were standard functionality that you would expect in a grid widget; sorting, filtering, cell editing, and also some less common features such as ‘column picker’ – that is, being able to choose which columns to display using a palette. We were also looking at column manipulation (resize, position), and file export.

What we were looking for in a dialog/window, was ability to display as modal dialog, and also dialog manipulation, ie moving, resizing.

These are the results of our research (click on image for better quality) :



The four yellow highlighted columns are the shortlist of candidates (we eventually went with extjs). Y=Feature is offered, N=Not offered, and we also have question marks in our analysis, where it wasn’t clear if it was offered or not. It would have been possible to dig deeper had we more time.

The bottom row is Stackoverflow References (SOR). Which is an indication of popularity and adoptability.

We also looked at cost, but that was not so much of an issue.

The frameworks can be broadly divided into those that are grid-only (SlickGrid, Flexigrid, Ingrid, Datatables and jqGrid), and those that offer a complete GUI framework (jQueryUI, extjs, yUI, DHTMLX, qooxdoo, Smartclient, Wakanda, Webix).

Some of these can also be lumped into a category as ‘framework for plugins’. jQuery and Dojo offer a foundation framework, on top of which plugins can be developed. In hindsight I guess AngularJS/Ember/Bootstrap and friends could also be lumped into that category.

A few words on some of the technologies :

jQueryUI – disappointing. Only really offers a pop-up dialog.

ExtJS – very comprehensive, high adoption, not exactly cheap.

yUI – fairly inconsistent, and has now been discontinued.

DHTMLX – simlar somewhat to extjs

qooxdoo – comprehensive and suprisingly free. The only thing that put us off was the low adoption rate, but hopefully that will change in the future.

SmartClient – Powerful rich components that costs money. Suffers from low adoption rate.

Dojo – Looks quite good. I think we didn’t go for this because of its plugin architecture.



Posted in JavaScript | Tagged , , , , , | Leave a comment

What is a Tree Table?

One of the most advanced GUI Components is the treetable. It looks really fancy, but what exactly is it and what scenarios can it be used with?

As the name suggests a treetable is a melding of a table and tree. The left side behaves like a tree component, nodes can be expanded or collapsed, and the right side behaves somewhat like a table with columns and rows of data.

To understand a treetable lets first have a look at a standard tree component.


A tree component is probably most famously known from Windows Explorer. Visually it is a series of indented nodes that may or may not be expandable.

There are two main ways of using a tree to represent a data structure, and then there is everything in between.

(1) Hierarchy

First it can represent a hierarchical structure. This is very evident in Windows Explorer. A folder can contain many folders, which can also contain many folders, and so on.

As a class diagram it would look like this:



This is a classic Composite Pattern. Each object instance can contain a number of instances of itself.

(2) One-to-Many

A tree component can be used to represent a one to many relationship. For example the Devices Manager in Windows lists a number of devices for a computer:



This model will only expand to one level.

More than one one-to-many relationships can also be represented. Each entity that contains a further entity can be expanded:




(Make sure to use icons judiciously to clearly illustrate to the end user the different entities. In the above example the device nodes should use a different icon).

(3) Combination

The previous two structures can be combined :



As you can see each folder can contain an instance of itself, however a file cannot, and can only be a leaf node.


A treetable supplies extra columns to the right of the tree. The intention is to illustrate that all the nodes and leafs in the tree share properties represented by the columns.

The below example shows how things might look using the ‘combined structure’ example above.


The property ‘type’ is common across all entities. However the property ‘size’ is common only to the file entity. Notice the empty space in the ‘size’ column for the folders.


A disadvantage of a treetable is that you can’t sort over all rows like in a normal table.

If you were to sort all the rows, then you would break the tree.

In Treetables you can only sort groups of children in nodes. For example in our previous example performing a sort on ‘size’ would only have an effect on a group of children belonging to a node like so :



Filtering a treetable presents the same issues as filtering a standard tree.

In a traditional table, performing a filter operation simply removes rows from the table. However when filtering a tree, the structure needs to be maintained all the way down to the root.

This maintaining of nodes in a filtering operation can create a lot of visual fluff.

For example :

Final Points

When to use:

· Hierarchical relationship with properties that need to be shown
· One-To-Many or Combination where the entities share a lot of properties.

When not to use:

· A full sort is required of rows.
· Small number of leafs, of which properties to be shown do not exist in the hierarchy entity. Ie A lot of real estate on the widget is not used. See below:



Framework Examples:

Posted in Usability | Tagged , , | Leave a comment

Loading Spinner with jQuery Promises

In your website you want to call a web service that performs some long running task. It may be loading a large set of data or performing a complex calculation on the server, so you want to animate a spinner or a loading indicator over the screen or component.

You can achieve this effect using jQuery promises.

Assume your web service call looks like this :

Because your function returns an AJAX object it can be used with the $.when function in jQuery.

If you start your spinner, then place your function in the $.when function, and then disabling the spinner in the always block, you will get seamless spinner activity.

All I am using for the spinner is an extjs loading panel using the call window.setLoading(true). However most frameworks have some kind of a spinner, or progress bar. Eg. angular has .

Promises tidy things up by not needing to have the callback method inside the ‘always’ block in the ajax code, making the method more reusable.

Posted in JavaScript | Tagged , , | Leave a comment

Layouts in Extjs

This is a quick overview of layouts in ExtJS.






The absolute layout positions an element using x,y coordinates.

This type of layout should be avoided, as resizing the panel will not resize the components. Only use it if you know what you are doing, and you know why you are doing it.




Allows slider layout. Most famously used in Microsoft Outlook.

A number of other options like ‘animate’ and ‘activeOnTop’ offer some interesting effects.




This layout anchors components to the edge of a container. Useful for forms. Takes string of two values that can be percent or offset pixels (as a negative number). For example, anchor: ‘50% -400’ will always be 50% wide from the left and -400 pixels from the bottom.

All components are anchored to the top left corner.

It is best used in conjunction with a hbox layout. The hbox defines a number of columns, and the anchor layout can lay out the components inside each of these columns.

Appears to ignore sizing of non-container components like labels.




For java developers very similar to BorderLayout in Swing. Useful for your application foundation, where you have a footer on the bottom, a navigation tree on the left, a header on top, and an editor in the middle.

If no width or height is defined for a container, then it will go down the hierarchy to find the widest element. In the above example, the west panel will have a width of 90 because a child element has its width set to 90. If another element is defined in this panel that is wider, then the panel will be set to the width of that element.

This layout is great, and should be the foundation for all your applications.

collapsable and split modes are a couple of interesting options.




For java developers, it’s just like Cardlayout in Swing.

Extjs has two flavours: wizard and tab. A tabbed panel is just a panel with a navigation tool above the panel. A wizard is a panel with ‘next’ and ‘back’ buttons usually on the bottom of the panel.




A Column layout defines a number of columns using percent or fixed width. Fixed width is calculated first, then percent.

Percentage values are calculated from the remainder after the fixed width elements have been added.

I wouldn’t bother too much with this layout. It is more or less deprecated as the hbox layout can fulfill everything that it does plus some more.

More info here.



This layout is very simple. It just fits component into container. It is basically the same as border layout, but with only the center part.



All elements are stretched across horizontally from top to bottom. If a fieldLabel exists, then a label will be added to the left of the component. The label will not be stretched, but the corresponding component will be stretched until reaching the labels minimum width.

A large number of forms can be done using this layout.



Aligns components along the horizontal axis. Used with the flex attribute. Flex is a ratio but represented as an integer number. The below example puts a next to b, but b will take up 75% of the space.

Fixed height items can also be added. Use align : ‘stretch’ or the components will anchor themselves to the left of the container.




Starts at top left corner and lays out component left to right based on the column value.

The table layout is not a sizing layout. Its cells expand depending on the components added. The cells ‘push out’ if you will. A layout like column or hbox will explicitly set the column widths, however this layout will not.

The attributes colspan and rowspan can be used to make cells stretch over more than one cell.


same as hbox but vertical! That’s all there is to say about this one.


Posted in extjs | Tagged | Leave a comment

Creating a simple RCP app with Netbeans and Maven

Netbeans offers a powerful way of modularising a desktop application. In this tutorial we will be creating a simple application. While this tutorial does not showcase many benefits of the netbeans rich client platform (more to come in other tutorials!), it should be enough to get someone started.

Create A New Project

In the file menu select ‘New Project’, then navigate to the Maven folder and choose Netbeans Application :


Select ‘Next’ and enter in a name and location for your project. Other Maven properties may also be entered such as Group Id and version.


In the last screen leave everything as it is :


Select ‘Finish’.
Netbeans should have created three new projects. They all should have the same name, but are appended differently with –app, -branding and –parent.


This is not actually a flat structure. If you expand the HappyDog-parent, you will see that the other two projects are defined as modules.


This is also reflected in the directory structure :


This is classic Maven inheritance in action. The two sub projects have the parent project defined in the tag. All Maven plugins and dependencies defined in the super project can also be accessed in the sub projects. Also, the parent POM file has the two sub projects listed as modules. Thus the association between parent and children is two way.

Clean and build the parent maven project HappyDog-parent (Right click HappyDog-parent and select ‘Clean and Build’). This action will also do the same to the sub modules.
You will often get an error when you first do this. This is because the RCP Archetype includes a Junit test that fails.


Just delete the file ‘’

Run Project

To compile, ‘clean and build’ on the parent HappyDog-parent.

To run, right click on HappyDog-app and select ‘Run’

If everything is set up right, then you should see this :


This is the basic frame for your application. All that is missing is modules you need to create, which we will look at in another tutorial.

Splash Screen

We want out own splash screen instead of the default blue Netbeans spash screen that always pops up. To do this, we need an image, and we need to save it somewhere in the src/main/resources folder in the branding project :


Now If you right click the branding project you should see a ‘Branding’ option listed in the context menu. Select this menu item.

A number of branding related features are presented. Select ‘Splash Screen’. In this tab you can browse to the image you want for the splash screen, as well as customize other graphical features.


NEXT Part2. In the next tutorial we will be adding functional modules to the simple application we created in this tutorial.


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


Thanks to Luann Snawder Photography for the dog image



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

ExtJS Filter Across All Grid Cells

ExtJS offers the ability to filter rows based on a column. By setting filters features in grid like so :

and defining filters on your columns like so :

You can get some pretty cool filtering functionality in your grids by clicking on the column drop down

However what if you wanted to filter grid rows globally (Ie. not restricted on a column) ?

The best way would be to add a textfield to your toolbar, or somewhere above your grid, and then have some method that filters across all rows based on the text you input.

The following code filters rows based on a text string.

Notes :

  1. To get all visible columns the codes uses the Ext.ComponentQuery functionality. This allows us to query components in the grid. Don’t ask me how it works, but the following returns just the columns that are visible : grid.query(‘gridcolumn:not([hidden])’);
  2. we need to iterate over each row, and then each cell so we have two loops here
  3. this function gets a column for a given field ID. We need this column later.
  4. A field may not be associated with a column. In extJS your store’s table of data may include additional columns that are not visible in the extjs grid.
  5. Now we check if the column has a renderer. With our filter function we can only filter on string values. Because column types may vary, the only way we can filter across strings is to get the rendered value. So we get the render function and pass our rec data value through it to get the string value.
  6. comparison
  7. set filterMeOut flag
  8. call the filterBy method on the store, passing in a custom function which does the comparison on our filterMeOut flag.

You can then call the method through a text field like this.

Posted in extjs | Tagged , | Leave a comment

Using JSDoc

Enhancing Javascript with JSDoc and JSDoc-checking IDEs

JSDoc is a way of documentating Javascript much like JavaDoc and Java annotations for Java. JSDoc does pretty much the same thing. It documents and annotates Javascript elements. However given that Javascript has much looser language rules than Java, these annotations can be leveraged to tighten parts of the code that may appear ambiguous particularly in a large project.

Probably the most useful features of JSDoc is the ability to sprinkle annotations amongst Javascript code which can then give semantic meaning to a function or variable. Using an advanced Javascript IDE, these annotations can be interpreted at compile time, and provide a kind of compile time checking, while also aiding in code-completion.

As of writing, the only IDE that can interpret JSDoc to any reasonable level is Webstorm. So all examples in this article are using the Webstorm IDE.

Simple Type Checking

Parameters types can be defined in the comments of the function. In this example we are annotating the x value being passed into the popup() method :


Now we are passing in a number :


Notice the squiggly line underneath the number 12345?

If we hover our mouse over the squiggly line, we should see a tooltip telling us what the problem is :







That’s pretty neat! Type checking at last! 

The @param JSDoc annotation allows us to say what we want passed into a function. We can also use this annotation on other primitive types such as number and boolean. Let’s have a look at how we can use the @param annotation on some more interesting objects.


Arrays and the Type Annotation

Javascript also has arrays and objects which can be considered types.

In the following example we are passing an array into a function :


In the above we create two types of fruit, and make an array for each of them. By using angular brackets, we can define the type of the array being passed in (Array.<Apple> )

Using the @type annotation we can annotate a variable. In our array example we are stating that we want out element to be of type Banana. As the second array being passed in is of type Apple, our JSDoc annotation is being infringed and we get the squiggly gray line again.

At the time of writing Webstorm does not support the JSDoc checking of the inner type of an array when being passed to a function (See the comments).

Parameter annotations on arrays can also help in code completion.


The JS compiler knows the element in an array is of type apple and shows the apple elements in the drop down list. This is unbelievably helpful!

Object Literals

Javascript objects can be very arbitrary objects. There is no real sense of type .

In the following example we are using the @param tag annotation again. Here we are annotating an object literal with two fields: name, and email which are both strings :








In the method body, a variable is assigned to the This variable has to be of type string, and matches up with the name type being passed into the method.

The following two examples show where the IDE complains and the squiggly gray lines start appearing again :








The @param annotation expects a number but we are passing in an array. Our @type annotation declares a variable to be string, but out @param says our user object can only be number.

In this case we are trying to treat one of the string fields as a number :









We can even document an array of literals.











In the following example the IDE will complain about the variable i being assigned to a string :












Again, code completion is enhanced. The IDE deals with the fields in an object literal :





Return Types

Returns can be annotated too.

In this example the IDE indicates there is a problem on the line where x is assigned the return value.








In this example the assignment is correct, however there is now a problem with the return statement. The IDE picks up on what is happening inside the function is incorrect.







Finally this example has everything working correctly :


Mixed Type

Because Javascript is duck typed, methods can accepted objects of various types. Sometimes you want to document a function and tell it that it can only accept objects of these ‘types’.












The IDE will complain at exactly this line : processFruit(new Orange())

Wrapping up..

I hope this introduction to JSDoc was useful. I don’t believe in strictly enforcing JSDoc across a whole project, but I do feel that JSDoc with a JSDoc-checking IDE becomes necessary as the project grows bigger. It allows one to treat javascript functions as black boxes, and prevents lots of picking around inside a function to try and understand what is going wrong.

This is particularly useful when you have Javascript project modules, ie. Common projects that are used across other javascript projects. A typical real world implementation I have come across is a table component that is used across many projects. It has a setModel(..) method, where the table model is passed into the component (Just like in Swing J ). Using a JSDoc-checking IDE, the developer would be notified that this object being passed in requires an array of columns, an array of entities, and a method getValueAt among other things.

Posted in JavaScript, Tutorial | Tagged , , , | 5 Comments