Java - best practice for getting values from complex dialogs

Hi,

Java is a language I use quite a bit, but much of my work hasn't required GUIs. Now, I'm developing an app which will contain a number of custom-made dialogs (which will obviously extend JDialog). Take a typical Options dialog which typically stores many parameters for a given application.

Simple dialogs that ship with Java allow to 'get' a given value. As they assume that the dialog only consists of obtaining a single piece of data, think JFileChooser.

So, what is the best way to cope with a dialog that could potentially hold hundreds of parameters? It's surely unfeasible to implement getters for each field. I was thinking either creating a hashmap object to hold key-value pairs, and 'get' that from the dialog. Or, I could encapsulate that a little more by creating a new class that manages these values, ProgramOptions or such like. That can be passed around between the dialog and the main app.

Re: Java - best practice for getting values from complex dialogs

Personally, I'd take your second option; create a new class to hold the objects (General rule of thumb in Java: creating new classes is a Good Thing (tm)). You could make this class Serializable or a JavaBean and then you would find it relatively simple to persist the data if you need to do so at a later time.

Another option would be to use the Java properties system to store system properties for the dialog. Then they can be accessed anywhere in the application without having a reference to the dialog itself. System.getProperty() and System.setProperty() can be used for this.

Re: Java - best practice for getting values from complex dialogs

Dusty wrote:

Personally, I'd take your second option; create a new class to hold the objects (General rule of thumb in Java: creating new classes is a Good Thing (tm)). You could make this class Serializable or a JavaBean and then you would find it relatively simple to persist the data if you need to do so at a later time.

Another option would be to use the Java properties system to store system properties for the dialog. Then they can be accessed anywhere in the application without having a reference to the dialog itself. System.getProperty() and System.setProperty() can be used for this.

Re: Java - best practice for getting values from complex dialogs

I doubt it's the most efficient method, but the way I've delt with things like this in the past is to set up the fields in either an Array or ArrayList, then use a for loop that gets the data and drops it into an Array/ArrayList which is then returned.

The only thing to remember with the above method is the castings involved, but you could implement your own subclasses of ArrayList that deal only with JTextFields and Strings seperately to get around that.

Re: Java - best practice for getting values from complex dialogs

sudman1 wrote:

The only thing to remember with the above method is the castings involved, but you could implement your own subclasses of ArrayList that deal only with JTextFields and Strings seperately to get around that.

Re: Java - best practice for getting values from complex dialogs

sudman1 wrote:

I doubt it's the most efficient method, but the way I've delt with things like this in the past is to set up the fields in either an Array or ArrayList, then use a for loop that gets the data and drops it into an Array/ArrayList which is then returned.

The only thing to remember with the above method is the castings involved, but you could implement your own subclasses of ArrayList that deal only with JTextFields and Strings seperately to get around that.

I personally wouldn't go for this exact style, instead I'd opt for a Map collection instead. This because you need to know which field is at which exact index. Imagine you make a form that has the fields: forename, surname, date of birth. And so you know that forename is .get(0) of your ArrayList, etc.

But then, you decide to put a new field, saluation (Mr, Miss, etc) at the beginning. This now shifts all your olds fields along by one, and so you need to edit that code. Sure, no biggie with a handful of fields, but large dialogs could cause headaches.

Re: Java - best practice for getting values from complex dialogs

arooaroo wrote:

But then, you decide to put a new field, saluation (Mr, Miss, etc) at the beginning. This now shifts all your olds fields along by one, and so you need to edit that code. Sure, no biggie with a handful of fields, but large dialogs could cause headaches.

In the arraylist, using an ArrayList with index values, you would use static final int constants to access the indexes of the array so they could be changed if necessary. In the Map case, you would use static final String constants as the keys, to ensure compile-time type checks (no spelling errors).

Nowadays, I'd suggest using typesafe enums to perform either of these tasks.

Re: Java - best practice for getting values from complex dialogs

Dusty wrote:

In the arraylist, using an ArrayList with index values, you would use static final int constants to access the indexes of the array so they could be changed if necessary. In the Map case, you would use static final String constants as the keys, to ensure compile-time type checks (no spelling errors).

Re: Java - best practice for getting values from complex dialogs

arooaroo wrote:

Dusty wrote:

In the arraylist, using an ArrayList with index values, you would use static final int constants to access the indexes of the array so they could be changed if necessary. In the Map case, you would use static final String constants as the keys, to ensure compile-time type checks (no spelling errors).

Ah, getcha.

In that case, I'd suggest using the int indexes, as it's much faster, unless the hashing algorithm the map collection uses is ungodly fast