> Hello
> I have a main window which pops up a modal dialog to ask the user for
[quoted text clipped - 15 lines]
>
> 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.
> Hello
> I have a main window which pops up a modal dialog to ask the user for
[quoted text clipped - 6 lines]
> 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.
Philipp - 20 Oct 2007 16:47 GMT
Thank you for your very complete answer.
Phil
Daniele Futtorovic - 23 Oct 2007 02:32 GMT
> Thank you for your very complete answer.
> Phil
You're welcome.
Dan.