A Frame that uses the Confirm dialog to verify quit

ConfirmTest.java
****************
import java.awt.*;
import java.awt.event.*;

/** A Frame that uses the Confirm dialog to verify that
 *  users really want to quit.
 *
 
public class ConfirmTest extends Frame {
  public static void main(String[] args) {
    new ConfirmTest();
  }

  public ConfirmTest() {
    super("Confirming QUIT");
    setSize(200, 200);
    addWindowListener(new ConfirmListener());
    setVisible(true);
  }

  public ConfirmTest(String title) {
    super(title);
  }

  private class ConfirmListener extends WindowAdapter {
    public void windowClosing(WindowEvent event) {
      new Confirm(ConfirmTest.this);
    }
  }
}
****************
Confirm.java
****************
dialog box with two buttons: Yes and No
****************
import java.awt.*;
import java.awt.event.*;

/** A modal dialog box with two buttons: Yes and No.
 *  Clicking Yes exits Java. Clicking No exits the
 *  dialog. Used for confirmed quits from frames.
 ********************

class Confirm extends Dialog implements ActionListener {
  private Button yes, no;

  public Confirm(Frame parent) {
    super(parent, "Confirmation", true);
    setLayout(new FlowLayout());
    add(new Label("Really quit?"));
    yes = new Button("Yes");
    yes.addActionListener(this);
    no  = new Button("No");
    no.addActionListener(this);
    add(yes);
    add(no);
    pack();
    setVisible(true);
  }

  public void actionPerformed(ActionEvent event) {
    if (event.getSource() == yes) {
      System.exit(0);
    } else {
      dispose();
    }
  }
}
***************

Permanent link to this article: http://bangla.sitestree.com/a-frame-that-uses-the-confirm-dialog-to-verify-quit/

Leave a Reply