Thursday 14 May 2015

Dialog in Java

  You can build your own dialog boxes using Frames, but the Dialog class enables you to create modal dialog boxes. A modal dialog box forces the user to deal with the dialog box before doing anything else. Although this generally isn't a good idea, certain types of actions, such as notifying a user of a problem, require user input before the program can do anything else.
Dialog boxes are containers, so you can add components to them, but their default Layout Manager is BorderLayout rather than FlowLayout. User interaction with dialog boxes can generate the WINDOW_DESTROY, WINDOW_ICONIFY, WINDOW_DEICONIFY, and WINDOW_MOVED events. As with other window-related classes, you should call the dispose method (inherited from Window) when the window is destroyed in order to free up window system resources.
The two dialog creator methods follow.
new Dialog(Frame a_parent, boolean modal_flag)
Creates a new dialog box that is modal if the second argument is TRUE.
new Dialog(Frame a_parent, String dialog_title, boolean modal_flag)
Functions like the preceding method, except that you can specify the name of the dialog box.

Unfortunately, Dialogs can be created only as children of Frames, as you can see from the arguments to the creator functions. This might make you think that you can't use Dialogs with your applets but, through the use of a minor subterfuge, you can. The secret, demonstrated in Listing 17.40, is to make a Frame but not show it. The applet extends the Dialog class and makes a simple Dialog
Listing 17.40. Using a dialog.
import java.awt.*;
import java.applet.Applet;


public class dialogs extends Applet
{
    Frame a_window;
    user_dialog real_dialog;
    Button show_dialog;


    public void init()
    {
        //You need a Frame to make a dialog but you don't have to display it.
        a_window = new Frame("Testing Dialogs");
        //This is a non-modal dialog, that is the user can 
        //switch between windows
        //while the dialog is active.  Change the false to true 
        //to make it modal and force
        //the user to deal with and dismiss it before doing anything else.
        real_dialog = new user_dialog(a_window,"The Sky Is Falling!",false); 
        show_dialog = new Button("Show Dialog");
        add(show_dialog); 
        }
    public boolean action(Event the_event, Object the_arg) {
        //There's only one button so we just make sure that a button was clicked
        if(the_event.target instanceof Button) {
            real_dialog.show(); 
        }
        return true;
    }
}
class user_dialog extends Dialog {
    Button b_ok, b_cancel;

    user_dialog(Frame a_frame,String the_title, boolean modal) {
        super(a_frame,the_title,modal); 
        FlowLayout fl; 

        b_ok = new Button("Ok"); 
        fl = new FlowLayout(); 
        setLayout(fl); 
        add(b_ok);
        //resize the dialog so the title, "The Sky Is Falling!" 
        //shows. You should use
        //FontMetrics here to see how many pixels wide that phrase is on a given
        //platform
        resize(200,40); 
    }

    public boolean action(Event the_event, Object the_arg) {
        if(the_event.target instanceof Button) {
            //Hide the dialog. If you're not going to use it again you should call 
            //window.dispose to get rid of it. Hide just makes it invisible
            hide(); 
        }
        return true;
    }
}


No comments:

Post a Comment