/* The DialogDemoLauncher applet displays a button. When the user clicks the button, a frame is opened that demos the MessageDialog class. */ import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.util.Date; public class DialogDemoLauncher extends Applet implements ActionListener { DemoFrame demo; // the main frame for the demo; this is null if the window is not open Button launchButton; // a button for opening the window public void init() { // initialize the applet setBackground(Color.white); setLayout(new BorderLayout()); launchButton = new Button("Launch DialogDemo"); add("Center",launchButton); launchButton.addActionListener(this); } // end init(); synchronized public void actionPerformed(ActionEvent evt) { // respond to a click on the button; if no window // is open, open one; if a window is open, close it // by calling its dispose() method. if (demo == null) { demo = new DemoFrame(); launchButton.setLabel("Close DialogDemo"); demo.addWindowListener(new FrameListener()); // This object will listen for the window // event that occurs when the window closes. } else { launchButton.setEnabled(false); // disable button while waiting // for window to close demo.dispose(); } } synchronized public void destroy() { // when the applet is destroyed, check if there is // a window open; if so, close it. if (demo != null) { demo.dispose(); demo = null; } } class FrameListener extends WindowAdapter { // This nested class defines an object that // listens for the WindowEvent that is generated // when the window closes for any reason. // The button's name is changed back to "Launch ShapeDraw" // and the button is enabled (after possibly being disabled in the // actionPerformed() method, if that is what caused the // window to close.) synchronized public void windowClosed(WindowEvent evt) { launchButton.setLabel("Launch ShapeDraw"); launchButton.setEnabled(true); demo = null; } } // end nested class FrameListener class DemoFrame extends Frame implements ActionListener, Runnable { // This class represents a main frame to be used in a demonstration of // modal dialogs created from the MessageDialog class. TextArea transcript; // message display area in window. Thread runner; // A thread for running the demo. volatile boolean isOpen; // Set to false when window closes. DemoFrame() { super("Dialog Demo"); setBackground(Color.white); setLayout(new BorderLayout(5,5)); transcript = new TextArea("This window is the main frame for a demonstration of modal dialogs. " + "When you click on the \"Show Dialogs\" button, a series of several " + "dialogs will appear. You have to click one of the buttons in each " + "dialog to make it go away.", 8,45,TextArea.SCROLLBARS_NONE); add("Center",transcript); Button bttn = new Button("Show Dialogs"); bttn.addActionListener(this); Panel panel = new Panel(); panel.add(bttn); add("South",panel); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent evt) { dispose(); // Close the window if user clicks the close box. } public void windowClosed(WindowEvent evt) { isOpen = false; // If window closes for any reason, stop any running thread. } } ); pack(); setLocation(80,100); setResizable(false); show(); isOpen = true; } public void actionPerformed(ActionEvent evt) { // When user clicks the button, start a thread to show some dialogs. runner = new Thread(this); runner.start(); } public void run() { // show some dialogs if (!isOpen) // if main window has closed somehow, stop the thread. return; transcript.setText("Creating first dialog...\n"); MessageDialog md1 = new MessageDialog(this, "To dismiss this dialog, you must click on one of the following buttons. " + "Until you do, the main Dialog Demo frame will be inaccessible.", "First", "Second", "Third"); if (!isOpen) return; transcript.append("Creading second dialog...\n"); MessageDialog md2 = new MessageDialog(this, "In the previous dialog, you clicked on the button named " + md1.getUserAction() + ". Would you like to see another dialog?", "Yes", "No"); if (!isOpen) return; if ("Yes".equals(md2.getUserAction())) { transcript.append("Creating third dialog...\n"); new MessageDialog(this,"The time is " + (new Date()).toString()); } else transcript.append("OK, no more dialogs\n"); if (!isOpen) return; transcript.append("Done.\n\n"); transcript.append("Click the button to start again."); } } // end nested class DemoFrame } // end DialogDemoLauncher