Dialog Box Using a Dialog Box To create
Dialog Box
Using a Dialog Box To create, display, and handle a dialog box, you must perform the following steps: o Create the dialog box object. o Create and set a layout manager for the dialog box. o Create controls and add them to the dialog box. o Call the dialog's show() method to display the dialog box. o When the user clicks the OK or Cancel button, call the dialog's hide() method to remove the dialog box from the screen. o Extract and process the data, if any, entered into the dialog box's controls.
Creating the Dialog Box o o o Java's dialog boxes are objects of the Dialog class. So, to create a dialog box object, you first call the Dialog class's constructor, like this: Dialog dialog = new Dialog(frame, title, modal); The constructor's three arguments refer to a frame window, the dialog box's title, and boolean value indicating whether the dialog box is modal (true) or modeless (false). A modal dialog box, which is the most common of the two types, retains the focus until the user dismisses it. This forces the user to respond to the dialog box before continuing with the program. A modeless dialog box can lose the focus to another window, which means that the user can switch to another window even while the dialog box is still on the screen.
Creating the Dialog Box's Layout o o Once you have the dialog box object created, you must give it a layout manager. If you fail to do this, any components you try to place in the dialog box will not appear. You perform this step exactly as you would for any other type of window or applet, by creating and setting the layout object: Flow. Layout layout = new Flow. Layout(); dialog. set. Layout(layout);
o o The next step is to create and add whatever controls you want to appear in the dialog box. You'll always have at least an OK button, with which the user can dismiss the dialog box: Button button = new Button("OK"); dialog. add(button);
Displaying the Dialog Box o o o Just like a frame window, a dialog box doesn't appear on the screen until you call its show() method, like this: dialog. show(); Once the dialog box is on the screen, the user can manipulate its controls in order to enter information into the dialog box's fields or to dismiss the dialog box from the screen.
Removing the Dialog Box o o o When the user clicks a dialog box's OK or Cancel buttons, that's your applet's signal to remove the dialog box from the screen, which you do by calling its hide() method: dialog. hide(); The hide() method removes the dialog box from the screen, but the dialog box and its controls remain in memory so that you can access them in order to extract whatever information the user may have entered into the dialog box.
Methods of the Dialog Class
Ex. Class Dialog. Demo { public static void main(String args[]){ Frame f=new Frame(“My Frame”); . . . Dialog d=new Dialog(f, ”Dialog Box”, true); . . . } }
File. Dialog Box import java. awt. *; import java. awt. event. *; class File. Di { public static void main(String args[]){ final Frame f=new Frame("My Frame"); f. set. Size(400, 400); f. set. Visible(true); f. add. Window. Listener(new Window. Adapter(){ public void window. Closing(Window. Event e){ f. dispose(); } }); File. Dialog fd=new File. Dialog(f, "My File Box"); fd. show(); } }
- Slides: 10