import java.awt.*;
import java.awt.event.*;
class NonModalYesNoDialog extends Dialog implements ActionListener {
private boolean isAnswered = false, isYes = false;
NonModalYesNoDialog( Frame frame, String question ) {
super(frame, false /* non modal */);
Label label = new Label(question);
label.setFont( new Font("Dialog",Font.PLAIN,20) );
add( "Center", label );
Panel yn = new Panel();
Button button = new Button("Yes");
button.addActionListener( this );
yn.add( button );
button = new Button("No");
button.addActionListener( this );
yn.add( button );
add("South", yn);
pack();
}
synchronized public boolean answer() {
while ( !isAnswered )
try { wait(); } catch (InterruptedException e) { /* error */ }
return isYes;
}
synchronized public void actionPerformed ( ActionEvent e ) {
isYes = e.getActionCommand().equals("Yes");
isAnswered = true;
notifyAll();
dispose();
}
public static void main(String[] s) {
Frame f = new Frame();
f.add( "Center", new Label("I'm the application") );
f.add( "South", new Button("You can press me?") );
f.pack();
f.show();
NonModalYesNoDialog query = new NonModalYesNoDialog( f, "Do you love me?");
query.show();
if ( query.answer() == true )
System.out.println("She loves me...");
else
System.out.println("She loves me not...");
}
}
|