home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jhelp.z / DialogWindow.java < prev    next >
Text File  |  1997-07-30  |  3KB  |  113 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. public class DialogWindow extends Frame 
  5.               implements WindowListener,
  6.                          ActionListener {
  7.     private boolean inAnApplet = true;
  8.     private SimpleDialog dialog;
  9.     private TextArea textArea;
  10.  
  11.     public DialogWindow() {
  12.         textArea = new TextArea(5, 40);
  13.         textArea.setEditable(false);
  14.         add("Center", textArea);
  15.         Button button = new Button("Click to bring up dialog");
  16.     button.addActionListener(this);
  17.         Panel panel = new Panel();
  18.         panel.add(button);
  19.         add("South", panel);
  20.     addWindowListener(this);
  21.     }
  22.  
  23.     public void windowClosed(WindowEvent event) {
  24.     }
  25.  
  26.     public void windowDeiconified(WindowEvent event) {
  27.     }
  28.  
  29.     public void windowIconified(WindowEvent event) {
  30.     }
  31.  
  32.     public void windowActivated(WindowEvent event) {
  33.     }
  34.  
  35.     public void windowDeactivated(WindowEvent event) {
  36.     }
  37.  
  38.     public void windowOpened(WindowEvent event) {
  39.     }
  40.  
  41.     public void windowClosing(WindowEvent event) {
  42.         if (inAnApplet) {
  43.             dispose();
  44.         } else {
  45.             System.exit(0);
  46.         }
  47.     }
  48.  
  49.     public void actionPerformed(ActionEvent event) {
  50.         if (dialog == null) {
  51.             dialog = new SimpleDialog(this, "A Simple Dialog");
  52.         }
  53.         dialog.setVisible(true);
  54.     }
  55.  
  56.     public void setText(String text) {
  57.         textArea.append(text + "\n");
  58.     }
  59.  
  60.     public static void main(String args[]) {
  61.         DialogWindow window = new DialogWindow();
  62.         window.inAnApplet = false;
  63.  
  64.         window.setTitle("DialogWindow Application");
  65.         window.pack();
  66.         window.setVisible(true);
  67.     }
  68. }
  69.  
  70. class SimpleDialog extends Dialog implements ActionListener {
  71.     TextField field;
  72.     DialogWindow parent;
  73.     Button setButton;
  74.  
  75.     SimpleDialog(Frame dw, String title) {
  76.         super(dw, title, false);
  77.         parent = (DialogWindow)dw;
  78.  
  79.         //Create middle section.
  80.     Panel p1 = new Panel();
  81.         Label label = new Label("Enter random text here:");
  82.         p1.add(label);
  83.         field = new TextField(40);
  84.     field.addActionListener(this);
  85.     p1.add(field);
  86.         add("Center", p1);
  87.  
  88.         //Create bottom row.
  89.         Panel p2 = new Panel();
  90.         p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
  91.         Button b = new Button("Cancel");
  92.     b.addActionListener(this);
  93.         setButton = new Button("Set");
  94.     setButton.addActionListener(this);
  95.         p2.add(b);
  96.         p2.add(setButton);
  97.         add("South", p2);
  98.  
  99.     //Initialize this dialog to its preferred size.
  100.     pack();
  101.     }
  102.  
  103.     public void actionPerformed(ActionEvent event) {
  104.     Object source = event.getSource();
  105.         if ( (source == setButton)
  106.            | (source == field)) {
  107.             parent.setText(field.getText());
  108.         }
  109.         field.selectAll();
  110.         setVisible(false);
  111.     }
  112. }
  113.