Lesson 5 | Choosing files with file dialog boxes |
Objective | Making the FileDialog visible |
Familiarize yourself with the setVisible() method.
You make the file dialog box visible the same way you make any other window visible. Just call the FileDialog
class's
setVisible(true)
method.
fd.setVisible(true);
At this point the operating system takes over and handles user interaction until the user either chooses a file or cancels.
Your program stops here and waits for the user to choose a file. When the user does choose a file, the file dialog box disappears from the screen and your program resumes.
Beginning with JDK 7, you can use FileDialog to select a list of files.
This functionality is supported by the following methods.
- setMultipleMode( ),
- isMultipleMode( ), and
- getFiles( )
methods.
Handling Events by Extending AWT Components
Java allows you to handle events by subclassing AWT components. Doing so allows you to handle events in much the same way as they were handled under the original 1.0 version of Java. Of course, this technique is discouraged, because it has the same disadvantages of the Java 1.0 event model, the main one being inefficiency. Handling events by extending AWT components is described in this section for completeness. When extending an AWT component, you must call the enableEvents( ) method of Component. Its general form is shown here:
protected final void enableEvents(long eventMask)
The eventMask argument is a bit mask that defines the events to be delivered to this component.
The AWTEvent class defines int constants for making this mask. Several are shown here:
You must also override the appropriate method from one of your superclasses in order to process the event. Be sure to also call the superclass version of the method.
The following sections provide simple programs that show how to extend several AWT components.
Fields
Modifier and Type |
Field and Description |
static long |
ACTION_EVENT_MASK The event mask for selecting action events. |
static long |
ADJUSTMENT_EVENT_MASK The event mask for selecting adjustment events.
|