views:

80

answers:

3

In our Swing application, we are using an automated testing tool in QA (Qf-Test) that works better when the swing components are named. (calling Component.setName). Although their automatic name assignments work reasonably well, we are introducing SwingX components into the project and that is causing some issues for the tool.

There are a lot of potential components on a screen (your typical business app data entry screens, but a lot of them - the app is on the complexity level of an ERP), what options are there for naming swing components in a reasonably unobtrusive manner?

+1  A: 

I typically store the fields in a JPanel in properties on the JPanel, like this:

private JLabel firstNameLabel;
private JTextField firstNameTextField;

At the end of the routine that instantiates and lays out these components, you could run a routine that uses java.lang.reflect to loop through each property of the panel. If a property descends from the class Component, you can call setName on it with the name of the property. So for example, it would end up calling:

this.firstNameLabel.setName("firstNameLabel");
this.firstNameTextField.setName("firstNameTextField");

...except through java.lang.reflect

You could also have the routine examine the bumpy case names of variables and replace it with standard case with spaces. This would make them more readable.

This approach will ensure that no matter what components you add to a panel, they will all get friendly names.

Erick Robertson
I had thought of that approach. My main concern is that it means that refactoring the field name breaks the naming. I guess annotations could be used in that case (if you want to force a name to remain the same after refactoring the field name).
Yishai
When the field name changes, wouldn't you want the name in the QA program to change along with it? I always name my field names after the label on the field, so they would only change if the label changed anyways. I don't know how often they change, but it would enforce consistency.
Erick Robertson
A: 

How are you building the interface? JFormDesigner and probably most of the other editors allow to set the name of the component automatically, which is very handy. Most of the time I don't have to think about the component names at all. Only exceptions are components that are not placed with JFormDesigner.

Carlos
The screens here are hand coded. For another component of the application we did use IntelliJ's GUI builder, but it doesn't have such a feature. It is definitely a nice feature of JFormDesigner, though.
Yishai
+1  A: 

I generally use a compound name for all my components; the name is built upon the parent name (eg a JDialog) and the field name (obtained by reflection). That makes component names unique in most cases (you still may have conflicts depending on how you name parents and how you use them, e.g. if you open several JInternalFrame and all have the same name...)

I have implemented a generic naming strategy (that does just that) in Guts-GUI.

By the way, component naming is not only interesting for UI testing, but it may also be useful for resource injection strategies (i18n of components).

jfpoilpret