FileDocCategorySizeDatePackage
ModalYesNoDialog.javaAPI DocExample1190Sat Feb 22 16:39:48 GMT 1997None

ModalYesNoDialog.java

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

class ModalYesNoDialog extends Dialog implements ActionListener {
	private boolean isYes = false;

	ModalYesNoDialog( Frame frame, String question ) {
		super(frame, true /* 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() {
		return isYes;
	}

	synchronized public void actionPerformed ( ActionEvent e ) {
		isYes = e.getActionCommand().equals("Yes");
		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("Can you press me?") );
		f.pack();
		f.show();

		ModalYesNoDialog query = new ModalYesNoDialog( 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...");
 	}
}