Dialog Boxes
Now that you have covered frames and frames with menus, the next step is to add a dialog box. Dialog boxes are typically used to verify some action the user selected is correct. e.g. if you try to delete a file, a dialog box should pop up asking "Are you sure?". The dialog box should disable all other input until the question is answered. Java's dialog boxes are containers that provide us with this type of ability.
| This next example illustrates how you can add
a dialog box to frames with menus. The applet creates the first frame as soon as you start this applet in your browser. It can be resized, maximized, or minimized. Click on File in the upper left corner to cause the drop-down menu to open. Select Quit to close the frame. A dialog box
opens and that frame becomes inactive until you click on
OK. |
Notice that the dialog box disables any interaction with the frame until the OK button is pressed. Some browsers also disable any interaction with the applet but this is not a reliable feature and is not defined in the specifications for the language. The dialog box is linked to the frame that created it and will stop interaction with that frame only. It is possible, depending on your browser, to create a new frame while the dialog box is active for a previous frame. That frame will be entirely independent of the dialog box and remain active until you select its quit menu item. You can then close the dialog boxes separately and dispose of the frames.
The code for this applet is explained below.
import java.applet.*;
import java.awt.*;
public class dialog extends Applet
{
Frame window = new dmoframe
("This is the first frame.");
|
Import all the facilities of
the AWT and applet that Java has to offer. Create an applet called frames. Create an instance of the frame class, initializing the title bar. Create an instance of the button class with a label. |
public void init()
{
window.show();
add(new Label("Hit this button to"));
add(new Button("Create another Frame"));
}
|
The init method adds a label The button created above is added to the applet. Labels are added to explain the behavior of the frame. The layout for the frame named window is set for FlowLayout. The default FlowLayout is center, top to bottom. Using a layout manager for frames is required. Labels are added to the newly created frame. |
public boolean action(Event evt, Object whatAction)
{
if((evt.target instanceof Button))
{
String buttonLabel = (String) whatAction;
if (buttonLabel == "Create another Frame")
{
Frame window = new dmoframe
("This is another frame.");
window.show();
}
}
return false;
}
}
|
When an action takes place
this method tests for it. If the action was an instance of Button, the string on the button is stored in buttonLabel If the string on the button is "Destroy the Frame", hide the frame named window and dispose of it. Change the label on btn to "Create a new Frame" and return true. If the string on the button is "Create a new Frame", resize to 300x200 and show the frame named window. Change the label on btn to "Destroy the Frame" and return true otherwise return false. |
| The Following Class constructs the Frame | |
|---|---|
class dmoframe extends Frame
{
dmoframe(String title)
{
super(title);
MenuBar topbar = new MenuBar();
setLayout(new FlowLayout());
setMenuBar(topbar);
Menu fileMenu = new Menu("File");
topbar.add(fileMenu);
fileMenu.add("Quit");
add(new Label
("Use the File menu to close this Window."));
resize(300,100);
}
|
We define a constructor class for the
frame The title of the frame is a parameter. Call the Frame super class to create the new frame and then create a new menu bar set the layout of the frame Place the menubar on the frame Add a quit option to the menu. Label the frame. Set the size. And our frame is initialized. |
public boolean action(Event evt, Object whatAction)
{
if (evt.target instanceof MenuItem)
{
if ((String)whatAction == "Quit")
{
Dialog dilog = new demodialog
(this,"You hit Quit!",true);
dilog.show();
hide();
dispose();
return true;
}
}
return false;
}
|
Our constructor class MUST handle all actions and events for the frame! If the menu is selected and the quit option is invoked.... Create an instance of the demodialog box to verify the option was selected for "this" frame titled "You hit quit" and show it . Since the modality of the dialog box is set to 'true', execution stops at this point until the dialog box is closed. Then the frame is hidden and disposed. If its not the action we want return false. |
public boolean handleEvent(Event evt)
{
if (evt.id == Event.WINDOW_DESTROY)
{
hide();
dispose();
return true;
}
return super.handleEvent(evt);
}
}
|
This handles the "close" event
for the frame. The WINDOW_DESTROY event occurs when you click on the X at the top right of the frame or you select Close in the control icon at the top left of the frame. In either case just close the frame without showing a dialog box. If the event is not the one we wanted we must return the nature of the event to the handler. If we don't the action method will not work. |
| The following class constructs the dialog box | |
class demodialog extends Dialog
{
demodialog
(Frame demoframe,String title,boolean yesno)
{
super(demoframe,title,yesno);
setLayout(new FlowLayout());
add(new Label("hit OK to Continue"));
add(new Button("OK"));
resize(200,100);
}
|
Define a constructor class for the
dialog box with three parameters. The frame, the title and the modality. Call the Dialog super class to create the new box and set the layout Place a label to prompt the user and a button on the box Set the size. And our dialog box is initialized. |
public boolean action(Event evt, Object whatAction)
{
if (evt.target instanceof Button)
{
String buttonLabel = (String) whatAction;
if (buttonLabel == "OK")
{
hide();
dispose();
return true;
}
}
return false;
}
}
|
This constructor class MUST handle all actions and events for the dialog box If the button is pressed.... Verify the OK button was pressed Then hide and dispose the dialog box Return true to signal the action was handled or If its not the action we want return false. |
If you would like to see the code for the
dialog applet click
here and, if you like, copy the
contents of the screen that loads in your browser , paste it in a
text editor and save it in a file called "dialog.java".
After compiling this program we need to create an HTML page with a
reference to the class file. Save the HTML code in the same
directory as the class file. When you load the HTML file in your
browser the applet will execute as below.
Notice that compiling this applet will create three different
class files. You will create dmoframe.class demodialog.class and
of course dialog.class from the applet's name. The demoframe
class from the last applet would be destroyed by compiling this
applet if we had named the frame constructor class demoframe.
That is why the dmoframe name was used. BE CAREFUL of this side
effect when compiling your own applets and applications.