home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / post1.0 / ui / example-1dot1 / FocusDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.8 KB  |  140 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.Applet;
  18. import java.awt.*;
  19. import java.awt.event.FocusListener;
  20. import java.awt.event.FocusEvent;
  21. import java.awt.event.ActionListener;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.WindowEvent;
  24. import java.awt.event.WindowAdapter;
  25.  
  26. public class FocusDemo extends Applet
  27.                implements FocusListener,
  28.                   ActionListener {
  29.  
  30.     TextArea display;
  31.     FocusWindow window;
  32.     Button b1, b2;
  33.  
  34.     public void init() {
  35.     b1 = new Button("Click to bring up a window.");
  36.     b1.addActionListener(this);
  37.  
  38.     b2 = new Button("Click to clear the display.");
  39.     b2.addActionListener(this);
  40.  
  41.     display = new TextArea(5, 20);
  42.     display.setEditable(false);
  43.  
  44.     setLayout(new BorderLayout());
  45.     add("North", b1);
  46.     add("Center", display);
  47.     add("South", b2);
  48.  
  49.     //Create but don't show window.
  50.     window = new FocusWindow(this);
  51.     }
  52.  
  53.     public void stop() {
  54.     window.setVisible(false);
  55.     }
  56.  
  57.     public void focusGained(FocusEvent e) {
  58.     displayMessage("Focus gained", e);
  59.     }
  60.  
  61.     public void focusLost(FocusEvent e) {
  62.     displayMessage("Focus lost", e);
  63.     }
  64.  
  65.     void displayMessage(String prefix, FocusEvent e) {
  66.     display.append(prefix
  67.                + ": "
  68.                + e.getSource() //XXX
  69.                + "\n"); 
  70.     }
  71.  
  72.     public void actionPerformed(ActionEvent e) {
  73.     if (e.getSource() == b1) {
  74.         window.pack();
  75.         window.setVisible(true);
  76.     } else {
  77.         display.setText("");
  78.     }
  79.     }
  80. }
  81.  
  82. class FocusWindow extends Frame {
  83.     class FocusWindowListener extends WindowAdapter {
  84.         public void windowClosing(WindowEvent e) {
  85.             setVisible(false);
  86.         }
  87.     }
  88.  
  89.     public FocusWindow(FocusListener listener) {
  90.     super("Focus Demo Window");
  91.     this.addFocusListener(listener);
  92.     this.addWindowListener(new FocusWindowListener());
  93.  
  94.     GridBagLayout gridbag = new GridBagLayout();
  95.     GridBagConstraints c = new GridBagConstraints();
  96.     setLayout(gridbag);
  97.  
  98.     c.fill = GridBagConstraints.HORIZONTAL;
  99.     c.weightx = 1.0;  //Make column as wide as possible.
  100.     TextField textField = new TextField("A TextField");
  101.     textField.addFocusListener(listener);
  102.     gridbag.setConstraints(textField, c);
  103.     add(textField);
  104.  
  105.     c.weightx = 0.1;  //Widen every other column a bit, when possible. 
  106.     c.fill = GridBagConstraints.NONE;
  107.     Label label = new Label("A Label");
  108.     label.addFocusListener(listener);
  109.     gridbag.setConstraints(label, c);
  110.     add(label);
  111.  
  112.     Choice choice = new Choice();
  113.     String choiceprefix = "Choice item #";
  114.     for (int i = 0; i < 10; i++) {
  115.         choice.addItem(choiceprefix + i);
  116.     }
  117.     choice.addFocusListener(listener);
  118.     gridbag.setConstraints(choice, c);
  119.     add(choice);
  120.  
  121.     c.gridwidth = GridBagConstraints.REMAINDER;
  122.     Button button = new Button("A Button");
  123.     button.addFocusListener(listener);
  124.     gridbag.setConstraints(button, c);
  125.     add(button);
  126.  
  127.     c.weighty = 1.0;   //Make this row as tall as possible.
  128.     c.weightx = 0.0;   
  129.     c.fill = GridBagConstraints.BOTH;
  130.     List list = new List();
  131.     String listprefix = "List item #";
  132.     for (int i = 0; i < 10; i++) {
  133.         list.addItem(listprefix + i);
  134.     }
  135.     list.addFocusListener(listener);
  136.     gridbag.setConstraints(list, c);
  137.     add(list);
  138.     }
  139. }
  140.