//Prompt.java //Demonstrates modal dialogs //Andy Harris, 05/00 import java.awt.*; import java.awt.event.*; public class Prompt extends Dialog{ Label lblMessage = new Label(); TextField txtResponse = new TextField(); Button btnOK = new Button("OK"); EventHandler evt = new EventHandler(this); String response = new String(""); public Prompt (Frame parent){ super(parent, "Message Box", true); setLayout(new GridLayout(0,1)); add(lblMessage); add(txtResponse); lblMessage.setAlignment(Label.CENTER); Panel pnlButton = new Panel(); add(pnlButton); pnlButton.setLayout(new FlowLayout()); pnlButton.add(btnOK); btnOK.addActionListener(evt); this.addWindowListener(evt); } //end constructor public void say(String message){ lblMessage.setText(message); txtResponse.setVisible(false); this.setLocation(100,100); this.setSize(200,200); this.setVisible(true); //this.pack(); } // end say public void ask(String message){ lblMessage.setText(message); txtResponse.setVisible(true); this.setLocation(100,100); this.setSize(200,200); this.setVisible(true); } // end ask public String getResponse(){ return response; } // end getResponse private class EventHandler extends WindowAdapter implements ActionListener{ Dialog theDialog; public EventHandler(Dialog d){ theDialog = d; } // end constructor public void actionPerformed(ActionEvent e){ // user clicked on OK button theDialog.setVisible(false); response = txtResponse.getText(); } // end actionPerformed public void windowClosing(WindowEvent e){ theDialog.setVisible(false); } // end windowClosing } // end EventHandler def } // end prompt class def