Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Dialog best practices

Thread view: 
Philipp - 16 Oct 2007 11:00 GMT
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
Lionel van den Berg - 16 Oct 2007 14:47 GMT
> 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.
Daniele Futtorovic - 19 Oct 2007 18:57 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.