Dialog best practices

P

Philipp

Hello
I have a main window which pops up a modal dialog to ask the user for
some input parameters.
What is the standard or best or cleanest way to get the parameters back
into the main window when the user clicks "OK" in the dialog?

I can think of a few ways:
1) the main window is passed to the dialog at construction. the main
window has a public method which is called from the dialog with the user
values as parameters.

2) the dialog is shown using a static method which returns the
parameters (JOptionPane.showInputDialog() does it that way)

3) the dialog is not dispose() but hidden on "OK" click, and has public
methods to get relevant parameters. So the main windows gets the values
after the dialog is hidden

4) Other methods?

What do you recommend?

Thanks.
Phil
 
L

Lionel van den Berg

Philipp said:
Hello
I have a main window which pops up a modal dialog to ask the user for
some input parameters.
What is the standard or best or cleanest way to get the parameters back
into the main window when the user clicks "OK" in the dialog?

I can think of a few ways:
1) the main window is passed to the dialog at construction. the main
window has a public method which is called from the dialog with the user
values as parameters.

2) the dialog is shown using a static method which returns the
parameters (JOptionPane.showInputDialog() does it that way)

3) the dialog is not dispose() but hidden on "OK" click, and has public
methods to get relevant parameters. So the main windows gets the values
after the dialog is hidden

4) Other methods?

Personally I prefer option 2. It provides a way of retrieving a value
without worrying about the implementation of any of the classes, just
the interface of the dialog. Though I would be extending JDialog to do
it, not using JOptionPane.

Lionel.
 
D

Daniele Futtorovic

Hello
I have a main window which pops up a modal dialog to ask the user for
some input parameters.
What is the standard or best or cleanest way to get the parameters back
into the main window when the user clicks "OK" in the dialog?

I can think of a few ways:
1) the main window is passed to the dialog at construction. the main
window has a public method which is called from the dialog with the user
values as parameters.

Evil. Don't do that. This binds the caller to the callee.
2) the dialog is shown using a static method which returns the
parameters (JOptionPane.showInputDialog() does it that way)

Simple, handy, clean.
3) the dialog is not dispose() but hidden on "OK" click, and has public
methods to get relevant parameters. So the main windows gets the values
after the dialog is hidden

The distinction between DISPOSE and HIDE shouldn't enter into it, as far
as I can surmise. The input dialog ought to store its state upon close
and make it available. The instance requesting the input information
should not be bothered with, nor should be given access to, how this
information is gathered UI-ways.
4) Other methods?

At any rate, retrieving the data from the dialog instance directly or
calling a static method of the dialog's class which'd hide the internals
is functionally equivalent: there's a wait() for the dialog to close.
The latter merely dispenses with the need to keep a reference to the
dialog instance. On the other hand, the static accessor provides only
limited information as to what happened with the dialog: either "OK" (or
its equivalent) got clicked and the input is returned, or "Cancel" got
clicked and null is returned. Access to the dialog instance would
provide more freedom in this matter (i.e. to combine chooser and input
dialogs).

The only improvement I could think of would be to pass a parameter
container, something like:

public interface DialogParameters {
Object[] getParameters();

Class getParameterType(Object parameter);

String getParameterDisplayName(Object parameter);

void setParameterValue(Object parameter, Object value);

Object getParameterValue(Object parameter);
}

This container could be used both with a static accessor as well as
directly with the dialog instance. You could combine this with a
DialogResult enum and have the static accessor made:

static DialogResult showInputDialog(DialogParameters dp){ }
What do you recommend?

There are but little differences between the two ways exposed above and,
while I might be wrong, Dialogs don't bear the look of a field facing or
in need of revolutionary changes to me. The static accessor sure is a
cute and handy one-liner.

DF.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top