Chapter 10. Swing Dialogs

In most applications, information occasionally needs to be displayed for a brief period of time, often just long enough for the user to read it and click OK or perhaps enter a value, such as a password. Swing provides the JOptionPane class to make creating such simple dialog boxes extremely easy — in many cases requiring just one line of code.

Applications may also serve more complex dialog needs, such as providing a property editor in which a set of related values can be modified, with an appropriate interface. Swing’s JDialog class supports such general-purpose dialogs. JDialogs can also be non-modal,[1] which means the user does not need to close the dialog before interacting with other application windows. When possible, implementing such an interface yields a more pleasant and productive user experience.

Even though JOptionPane makes it very easy (for the programmer) to pop up a dialog, bear in mind that this will disrupt the flow of activity for users and force them to deal with the dialog before they can proceed with their underlying task. While this is sometimes unavoidable or even appropriate, it is usually worth trying to find less disruptive alternatives (direct manipulation, a non-modal floating notification, or some other non-modal approach). This may require more work on the part of the developer but will result in a better application. And if the application is widely adopted, the benefits are multiplied across the entire user base.

[1]

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.