Retrieving Information from a Dialog via Callbacks

For non-modal dialogs you need to use callbacks:

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

public class YesNoDialog extends Dialog implements ActionListener {

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

  public YesNoDialog(Frame parent, String message, 
                     ResultListener listener) {
  
    super(parent, true);
    this.result = listener;
    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.pack();
    this.setLocation(100, 200);
    this.pack();
  
  }  
  
  public void actionPerformed(ActionEvent evt) {
    this.setVisible(false);
    this.dispose();
    if (evt.getSource == yes) result.notify(true);
    else result.notify(false);
  }
  
}

public interface ResultListener {

  public void notify(boolean result);
  
}

Previous | Next | Top | Cafe au Lait

Copyright 2004, 2005, 2006 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified September 16, 2006