Subclassing java.awt.Dialog

The previous example was a little artificial. Normally you create your own subclass of Dialog and instantiate that subclass from the main program. For example one of the simpler common dialogs is a notification dialog that gives the user a message to which they can say OK to signify that they've read it. The following program is such a Dialog subclass.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class YesNoDialog extends Dialog implements ActionListener {

  private Button yes = new Button("Yes");
  private Button no = new Button("No");

  public YesNoDialog(Frame parent, String message) {
  
    super(parent, true);
    this.add(BorderLayout.CENTER, new Label(message));
    Panel p = new Panel();
    p.setLayout(new FlowLayout());
    yes.addActionListener(this);
    p.add(yes);
    no.addActionListener(this);
    p.add(no);
    this.add(BorderLayout.SOUTH, p);
    this.setSize(300,100);
    this.setLocation(100, 200);
    this.pack();
  
  }  
  
  public void actionPerformed(ActionEvent evt) {
    this.hide();
    this.dispose();
  }
  
}


Previous | Next | Top | Cafe au Lait

Copyright 1997-1999, 2002, 2005 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified July 1, 2005