Dialog Boxes: N Le RN in G N Te
Dialog Boxes: N Le RN in G N Te
et
g
Le ar nin
Sa ms.n
Ce nte r
This menu has four items: one each for the colors red, blue, and green (which, when selected,
change the background of the window), and one checkbox menu item for reversing the color
of the text (to white). To handle these menu items, you need an method:
Figure 14.2.
A menu.
Dialog Boxes 14
Dialog boxes are functionally similar to frames in that they pop up new windows on the screen.
However, dialog boxes are intended to be used for transient windows—for example, windows
that let you know about warnings, windows that ask you for specific information, and so on.
Dialogs don’t usually have titlebars or many of the more general features that windows have
(although you can create one with a titlebar), and they can be made nonresizable or modal.
285
S
S
DAY
F
R
W
T
M
NEW A modal dialog prevents input to any of the other windows on the screen until that dialog
TERM is dismissed.
The AWT provides two kinds of dialog boxes: the class, which provides a generic dialog,
and , which produces a platform-specific dialog to choose files to save or open.
To create a generic dialog, use one of these constructors:
creates an initially invisible dialog, attached to the current
frame, which is either modal ( ) or not ( ).
is the same as the previous constructor, with the
addition of a titlebar and a title indicated by the string argument.
Note that because you have to give a dialog a argument, you can attach dialogs only to
windows that already exist independently of the applet itself.
The dialog window, like the frame window, is a panel on which you can lay out and draw UI
components and perform graphics operations, just as you would any other panel. Like other
windows, the dialog is initially invisible, but you can show it with and hide it with .
Let’s add a dialog to that same example with the popup window. You’ll add a menu item for
changing the text of the window, which brings up the Enter Text dialog box (see Figure 14.3).
Figure 14.3.
The Enter Text dialog.
To add this dialog, first add a menu item to that window (the constructor method for the
class) to change the text the popup window displays:
In that same method, you can create the dialog and lay out the parts of it (it’s invisible by default,
so you can do whatever you want to it and it won’t appear on screen until you show it):
286
abcd
et
g
Le ar nin
Sa ms.n
Ce nte r
The action of choosing the menu item you just added brings up the dialog; choosing the OK
button dismisses it. So you need to add behavior to this class’s action method so that the dialog
works right. To the menu item tests, add a line for the new menu item:
Then, because is a button, you have to add a special case for that button separate from the
menu items. In this special case, set the text of the window to the text that was typed into the
text field, and then hide the dialog again:
File Dialogs
provides a basic file open/save dialog box that enables you to access the file system.
The class is system-independent, but depending on the platform, the standard Open
File dialog is brought up.
Note: For applets, you can bring up the file dialog, but due to security restrictions
you can’t do anything with it (or, if you can, access to any files on the local system
is severely restricted). is much more useful in stand-alone applications.
DAY
F
R
W
T
M
When the reader chooses a file in the file dialog and dismisses it, you can then get to the file they
chose by using the and methods; both return strings indicating the
values the reader chose. You can then open that file by using the stream and file handling
methods (which you’ll learn about next week) and then read from or write to that file.
Window Events
Yesterday, you learned about writing your own event handler methods, and you noted that the
class defines many standard events for which you can test. Window events are part of that
list, so if you use windows, these events may be of interest to you. Table 14.1 shows those events.
288
abcd
et
g
Le ar nin
Sa ms.n
Ce nte r
This method does five things:
It creates a new frame to hold the applet.
It creates an instance of the class that defines that method.
It duplicates the applet environment calls to and .
It adds the applet to the frame and resizes the frame to be 300 pixels square.
It shows the frame on the screen.
By using this mechanism, you can create a Java program that can function equally well as an
applet or an application—just include for applets and for applications.
If you do create an application that uses this mechanism, be careful of your methods that
get parameters from an HTML file. When you run an applet as an application, you don’t have
the HTML parameters passed into the method. Pass them in as command-line
arguments, instead, and handle them in your method. Then set a flag so that the
method doesn’t try to read parameters that don’t exist.
Networking in Java
Networking is the capability of making connections from your applet or application to a system
over the network. Networking in Java involves classes in the package, which provide
cross-platform abstractions for simple networking operations, including connecting and
retrieving files by using common Web protocols and creating basic Unix-like sockets. Used in
conjunction with input and output streams (which you’ll learn much more about next week),
reading and writing files over the network becomes as easy as reading or writing to files on the
local disk.
There are restrictions, of course. Java applets cannot read or write from the disk on the machine
that’s running them. Depending on the browser, Java applets may not be able to connect to
systems other than the one upon which they were originally stored. Even given these restrictions,
you can still accomplish a great deal and take advantage of the Web to read and process
information over the net. 14
This section describes three ways you can communicate with systems on the net:
, which enables an applet to tell the browser to load and link to
another page on the Web
, a method that opens a connection to a URL and enables you to extract
data from that connection
289
S
S
DAY
F
R
W
T
M
Getting a URL object is the hard part. Once you have one, all you have to do is pass it to the
browser. Do this by using this single line of code, where is the URL object to link to:
The browser that contains your URL will then load and display the document at that URL.
Listing 14.2 shows a simple applet that displays three buttons that represent important Web
locations (the buttons are shown in Figure 14.4). Clicking on the buttons causes the document
to be loaded to the locations to which those buttons refer.
290