// // another example of the Java AWT, showing use of Dialog class. // see class definition and main method for details. // import java.awt.* ; import java.awt.event.* ; // // a DialogExample object is a CloseableFrame containing a Button // and a Label. when the Button is pressed, a Dialog pops up // (modal or not depending on argument to constructor). the // Dialog contains two Button objects; when one is pressed, // the Dialog displays the result in the Label and self-destructs. // class DialogExample extends CloseableFrame { Label display ; Dialog dlg ; DialogExample(boolean modal) { super("DialogExample") ; setLayout(new BorderLayout()) ; makeDialog(modal) ; display = new Label("", Label.CENTER) ; add(display, "North") ; Button b = new Button("press here for dialog") ; add(b, "South") ; // action to be performed when button is pressed -- // show dialog, and also print to standard output // to confirm that button was pressed. note // that if dialog is modal, nothing happens here // until the user makes a selection from the // dialog. b.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("button pressed") ; setDisplay("no answer yet") ; dlg.show() ; } } ) ; pack() ; show() ; } // sets the text in the display. void setDisplay(String s) { display.setText(s) ; } // makes the requested dialog (a separate Window). void makeDialog(boolean modal) { dlg = new Dialog(this, "A dialog", modal) ; dlg.setLayout(new BorderLayout()) ; // put some text in the dialog window. Label dlgL = new Label( "Daisy, Daisy, give me your answer, do:", Label.CENTER) ; dlg.add(dlgL, "Center") ; // add two buttons to the dialog window, grouped // using a panel. Panel yn = new Panel() ; Button yes = new Button("yes") ; Button no = new Button("no") ; yn.add(yes) ; yn.add(no) ; dlg.add(yn, "South") ; // actions to be performed when dialog buttons are // pressed -- modify display in original // frame, and self-destruct. yes.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setDisplay("answer is yes") ; dlg.dispose() ; } } ) ; no.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setDisplay("answer is no") ; dlg.dispose() ; } } ) ; // note that we wait for original frame to show // (pop up) the dialog. dlg.pack() ; } // // ---- main --------------------------------------------------- // // command-line argument is y/n (whether dialog is modal). // public static void main(String[] args) { if (args.length < 1) { System.out.println("Argument is y/n") ; System.exit(-1) ; } boolean modal = args[0].equals("y") ; DialogExample d = new DialogExample(modal) ; } }