Thursday, August 14, 2008

Simplified GUI Code Generation

Fully Qualified Class Names
When we design JFrame, JPanels and any other GUI component in NetBeans the generated code contains fully qualified class names of the used classes.

For example let us create a sample JFrame form and add some components to that form through the form designer.

Let us keep the components to minimum as this is just an example. So we have added one label one text field and a button.
When we can see in the folded generated code for init components is all the components are defined and instanciated using the fully qualified class names.

More Human way of code writing
When we write code manually usually we do not write the fully qualified names, we make use of the import option and import all the classes at the top and use just the class names.

NetBeans IDE 6.5 Beta onwards we have a way to do just that. Now we can get simplified code through configuring NetBeans IDE and asking for simplified code.

Select the root component of the form from the Inspector panel. For our example we have clicked on the Form MainForm root node in the tree as shown above.

Now in the properties panel...

There is a new property named Generate Full Classnames and which will be by default checked if you havent done any setting yet. Let us uncheck that checkbox to make our code simpler.

Now the code looks simpler and easy to understand. This is how I will write the code if I write it manually, isn't it?

If you like this setting and if you want it to become the default behavior of the IDE then we have a global setting for it in the Tools > Options dialog box. Just select the Miscelleneous section and the GUI Builder tab. Ensure that Generate Fully Qualified Names of Classes checkbox is unckecked.

With Regards
Tushar Joshi, Nagpur

1 comment: