FileDocCategorySizeDatePackage
YesNoDialog.javaAPI DocExample4904Mon Sep 22 13:30:30 BST 1997None

YesNoDialog

public class YesNoDialog extends Dialog

Fields Summary
protected ActionListener
listeners
This field will hold a list of registered ActionListeners, thanks to the magic of AWTEventMulticaster
Constructors Summary
public YesNoDialog(Frame parent, String title, String message, String yes_label, String no_label, String cancel_label)

    // Create a modal dialog with the specified title and parent
    super(parent, title, true);

    // Specify a LayoutManager for it
    this.setLayout(new BorderLayout(15, 15));

    // Put the message label in the middle of the window.
    // Note: MultiLineLabel is a custom component defined later in the chapter
    this.add("Center", new MultiLineLabel(message, 20, 20));

    // Create an action listener for use by the buttons of the dialog.
    // When a button is pressed, this listener first closes the dialog box.
    // Then, it passes the event on to the listeners registered for the
    // dialog box, after changing the event source from the individual button
    // to the dialog box itself.  Since events are immutable, it must create
    // a new ActionEvent object in order to change the source.  It passes
    // this new event to the listeners by calling actionPerformed() on the
    // AWTEventMulticaster object stored in the listeners field (see below).
    ActionListener listener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        YesNoDialog.this.dispose();   // pop down dialog
        if (listeners != null)        // notify all registered listeners
          listeners.actionPerformed(new ActionEvent(YesNoDialog.this,
                                                    e.getID(),
                                                    e.getActionCommand()));
      }
    };

    // Create a panel for the dialog buttons and put it at the bottom
    // of the dialog.  Specify a FlowLayout layout manager for it.
    Panel buttonbox = new Panel();
    buttonbox.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 15));
    this.add("South", buttonbox);

    // Create each specified button, specifying the action listener
    // and action command for each, and adding them to the buttonbox
    if (yes_label != null) {              // if a label was specified...
      Button yes = new Button(yes_label);     //   create button
      yes.setActionCommand("yes");            //   set action command
      yes.addActionListener(listener);        //   set listener
      buttonbox.add(yes);                     //   add button to the panel
    }
    if (no_label != null) {
      Button no = new Button(no_label);
      no.setActionCommand("no");
      no.addActionListener(listener);
      buttonbox.add(no);
    }
    if (cancel_label != null) {
      Button cancel = new Button(cancel_label);
      cancel.setActionCommand("cancel");
      cancel.addActionListener(listener);
      buttonbox.add(cancel);
    }

    // Finally, set the dialog to its preferred size.
    this.pack();
  
Methods Summary
public voidaddActionListener(java.awt.event.ActionListener l)
Register an action listener to be notified when a button is pressed AWTEventMulticaster makes this easy.


                    
      
    listeners = AWTEventMulticaster.add(listeners, l);
  
public static voidmain(java.lang.String[] args)
A main method that demonstrates how to use this class, and allows testing

    // Create, size, and show a frame because dialogs require a frame parent.
    Frame f = new Frame("InfoDialog Test");
    f.setSize(100, 100);
    f.show();

    // Create an instance of InfoDialog, with title and message specified
    YesNoDialog d =
      new YesNoDialog(f, "YesNoDialog Test",
                      "There are unsaved files.\n" +
                      "Do you want to save them before quitting?",
                      "Yes, save and quit",
                      "No, quit without saving",
                      "Cancel; don't quit");

    // Register an action listener for the dialog.  This one just prints
    // the results out to the console.
    d.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("The user pressed: " + e.getActionCommand());
      }
    });

    // Now pop the dialog up.  It will pop itself down automatically.
    d.show();
  
public voidremoveActionListener(java.awt.event.ActionListener l)
Remove an action listener from our list of interested listeners

    listeners = AWTEventMulticaster.remove(listeners, l);