Swing Components / Dialogs
Subtitles of the Movie
In every application, the time comes when you've got to either tell the user something or ask a simple question. You may need to ask the user whether the data should be saved or if the program should continue running. At the very least, you need to be able to inform the user of an error condition. Java Swing has the JOptionPane class to make building pop-ups very easy. Let me show you a simple example. This is a simple program that only displays a window with a push button and the push button pops up a dialog with an OK button. Now, that pop-up dialog is built and displayed with one line of code and it's done with a call here to a static method. So you don't even have to create an object. The exact form of the dialog is controlled by the exact form of the method call. This is the simplest form I'm showing you here. Let me show you. This is the main window of the program, which contains nothing but one button used to pop up the dialog. When the dialog pops up, it displays over the center of the parent window. The two arguments used on the method call were the parent window in this case the JFrame and the string to be displayed. Now, notice the modality. The parent window can be viewed but it has been disabled. You can't do anything with it until you close the dialog. That's a safety measure. You can't go on with your program until the user responds. In fact, the method call that you made doesn't return until the user responds. By selecting the proper option dialog method, you can give the user more than once choice and the method can return a code that will indicate which choice was made. The return value is an integer indicating which button was selected. In this example, the show confirm dialog method was used. The dialog has three buttons; yes, not and cancel, numbered 0, 1 and 2. The method doesn't return the number until the user makes a selection, so the program runs like this. When you look at the documentation for the JOption dialog, you'll find a number of methods that display dialogs. Their names will tell you which type they are. They fall into one of four categories. The first one I showed you was a message dialog. It only needed an OK from the user. A confirm dialog is like the second one I showed you. It's for asking the user to confirm an action that's about to be taken. It gives the user the option of preventing the action. An example of this would be the famous are you sure window that pops up. Some dialogs request information input from the user. This is usually something like a file name. I'll show you how that works here in just a minute. This isn't really a new type. It's a method of constructing a dialog. You get to choose the number of buttons, the button labels, the displayed icons and so on. You can use it to build custom dialogs with the buttons labeled for special cases. The JOptionPane dialogs all contain an icon designed to represent the type of message. The icons vary a bit from one operating system to the next, but these are the ones that show up on Windows. This is the icon for information, as you saw in an earlier example. And this is the icon shown when a dialog is being displayed to ask a question. This icon is displayed when warning the user about something. It means nothing is wrong, but something is going on the user should be aware of this is the icon that represents an error message. There is a total of 15 static methods for popping up dialogs. Here is an example of one that requires input. This dialog will expect a choice of one number from its list. The program specifies the title of the dialog and a prompt for input, as well as the icon specifying the message type. It works like this.
Tutorial Information
| Course: | Java 6 |
| Author: | Arthur Griffith |
| SKU: | 33858 |
| ISBN: | 1-934743-59-3 |
| Release Date: | 2008-02-29 |
| Duration: | 7 hrs / 92 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 