Retrieving Information from a Dialog via Modality
- A modal dialog blocks all input
to all other windows of the application
except those that have the dialog for a parent
- When a modal dialog is shown the calling thread blocks
until the dialog is hidden.
import java.awt.*;
import java.awt.event.*;
public class YesNo implements ActionListener {
private Button yes = new Button("Yes");
private Button no = new Button("No");
private boolean result;
public static boolean ask(Frame parent, String message) {
Dialog d = new Dialog(parent, true);
d.add(BorderLayout.CENTER, new Label(message));
Panel p = new Panel();
d.setLayout(new FlowLayout());
YesNo listener = new YesNo();
yes.addActionListener(listener);
p.add(listener.yes);
no.addActionListener(listener);
p.add(listener.no);
d.add(BorderLayout.SOUTH, p);
d.pack();
d.setLocation(100, 200);
d.setVisible(true);
return listener.result;
}
public void actionPerformed(ActionEvent evt) {
if (evt.getSource == yes) result = true;
else result = false;
this.setVisible(false);
this.dispose();
}
}
Copyright
1997-1999, 2002, 2005, 2006 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified
November 10, 2006