0% found this document useful (0 votes)
29 views

Dialog Boxes: N Le RN in G N Te

The document discusses different types of windows in Java including dialog boxes, file dialogs, and window events. It describes how to create modal and non-modal dialog boxes and file dialogs in Java and handle related events.

Uploaded by

Manoj Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Dialog Boxes: N Le RN in G N Te

The document discusses different types of windows in Java including dialog boxes, file dialogs, and window events. It describes how to create modal and non-modal dialog boxes and file dialogs in Java and handle related events.

Uploaded by

Manoj Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

abcd

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

14 Windows, Networking, and Other Tidbits

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.

To create a file dialog, use the following constructors:


14
creates an Open File dialog, attached to the given frame,
with the given title. This form creates a dialog to load a file.
also creates a file dialog, but that integer argument is
used to determine whether the dialog is for loading a file or saving a file (the only
difference is the labels on the buttons; the file dialog does not actually open or save
anything). The possible options for the mode argument are and
.
287
S
S

DAY
F
R
W
T
M

14 Windows, Networking, and Other Tidbits

After you create a instance, use to display it:

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.

Table 1 4 . 1: Window Events from the Class.


Generated when a window is destroyed (for example, when the
browser or applet viewer has quit)
Generated when the window is brought forward from behind other
windows
Generated when the window is iconified
Generated when the window is restored from an icon
Generated when the window is moved

Using AWT Windows


in Stand-Alone Applications
Because frames are general-purpose mechanisms for creating AWT windows with panels, you
can use them in your stand-alone Java applications and easily take advantage of all the applet
capabilities you learned about this week. To do this, write your application as if it were an applet
(inheriting from the class and using threads, graphics, and UI components as necessary),
and then add a method. Here’s one for a class called :

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

14 Windows, Networking, and Other Tidbits

The socket classes, and , which enable you to open standard


socket connections to hosts and read to and write from those connections

Creating Links Inside Applets


Probably the easiest way to use networking inside an applet is to tell the browser running that
applet to load a new page. You can use this, for example, to create animated image maps that,
when clicked, load a new page.
To link to a new page, you create a new instance of the class URL. You saw some of this when
you worked with images, but let’s go over it a little more thoroughly here.
The class represents a uniform resource locator. To create a new URL, you can use one of
four different forms:
creates a new URL object, given a protocol (http,
ftp, gopher, file), a host name ( , ), a port number ( for
http), and a filename or pathname.
does the same thing as the previous form, minus the
port number.
creates a URL, given a base path and a relative path. For the base,
you can use for the URL of the current HTML file, or
for the URL of the Java class file. The relative path will be tacked onto
the last directory in those base URLs (just like with images and sounds).
creates a URL object from a URL string (which should include the
protocol, hostname, and filename).
For that last one (creating a URL from a string), you have to catch a malformed URL exception,
so surround the URL constructor with a :

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

You might also like