There are times when an application needs to get the attention of its user. It might be the user has entered in some data incorrectly or the application needs some help in determining what to do next. The most direct way is through the use of a dialog box.
When using a computer, users have become used to applications asking them for information or telling them something is wrong. You've probably experienced pop-up windows asking you for a username and password, telling you a file can't be found or giving you any number of error messages right before an application crashes. These pop-up windows are called dialog boxes.
A dialog box is a window that's separate from an application's main interface. It floats in front of the main application window and can be moved around without the application moving with it. A user can't help but notice a new window when it suddenly appears in front of them. This makes them very effective in conveying information to the user or collecting information from the user.
Remember, even though a dialog box acts independently from an application it is still very much part of the application. Think of it as a child window to the application's main window.
Modal or Modeless
One term you should be familiar with when dealing with dialog boxes is
modal dialog box is one that refuses to be ignored. It won't let a user interact with the rest of the application until it has been dealt with. For example, a dialog box asking for a username and password is typically
modal because there's little point in trying to use an application unless you're logged in. Until you have entered in your username and password the window will not disappear and will demand your attention.
When a dialog box is not
modal it's known as
Modeless boxes are still floating independently but they don't stop a user interacting with the rest of the application's interface. They are not as attention seeking as their
modal counterparts. For example, in a drawing application there tends to be a
modeless dialog, called a toolbox, that offers different drawing modes to a user.
It's easy to get the terms
modeless mixed up. I think of
modal boxes as being tenacious, they don't stop until they get what they want, so I tend to imagine putting them into a tenacious mode. If I don't want that bulldog attitude then the dialog must be modeless.
Dialog Boxes in Java
Java offers several classes for creating dialog boxes in it's API. This means that all the heavy coding has been done for you and all that's needed to create a dialog box is to give it some parameters (i.e., the message, a title for the dialog box, etc..). The classes in Java are:
- JOptionPane - This class is most commonly used for dialog boxes as it can be used to create message dialogs (i.e., displays a message) as well as input dialogs (i.e., asks a user to input information).
- JColorChooser - Allows a user to pick a color from a range of colors.
- JFileChooser - Helps a user navigate around a file system to find or save a file to a particular location.
- ProgressMonitor - Provides feedback to a user on how much progress a particular task has achieved.