home *** CD-ROM | disk | FTP | other *** search
/ Freesoft 1997 May / Freesoft_1997-05_cd.bin / recenz / PROGRAM / JAVADRAW / iavadraw301_inst.exe / data.z / ListDemo.java < prev    next >
Text File  |  1997-05-20  |  4KB  |  109 lines

  1. /*
  2.  * Copyright (c) 1995, 1996 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.awt.*;
  18. import java.applet.Applet;
  19.  
  20. public class ListDemo extends Applet {
  21.     TextArea output;
  22.     List spanish, italian; 
  23.  
  24.     public void init() {
  25.  
  26.         //Build first list, which allows multiple selections.
  27.         spanish = new List(4, true); //prefer 4 items visible
  28.         spanish.addItem("uno");
  29.         spanish.addItem("dos");
  30.         spanish.addItem("tres");
  31.         spanish.addItem("cuatro");
  32.         spanish.addItem("cinco");
  33.         spanish.addItem("seis");
  34.         spanish.addItem("siete");
  35.  
  36.         //Build second list, which allows one selection at a time.
  37.         italian = new List(); //Defaults to none visible, only one selectable
  38.         italian.addItem("uno");
  39.         italian.addItem("due");
  40.         italian.addItem("tre");
  41.         italian.addItem("quattro");
  42.         italian.addItem("cinque");
  43.         italian.addItem("sei");
  44.         italian.addItem("sette");
  45.  
  46.         //Add lists to the Applet. 
  47.         GridBagLayout gridBag = new GridBagLayout();
  48.         setLayout(gridBag);
  49.  
  50.         //Can't put text area on right due to GBL bug
  51.         //(can't span rows in any column but the first). 
  52.         output = new TextArea(10, 40);
  53.         output.setEditable(false);
  54.         GridBagConstraints tc = new GridBagConstraints();
  55.         tc.fill = GridBagConstraints.BOTH;
  56.         tc.weightx = 1.0;
  57.         tc.weighty = 1.0;
  58.         tc.gridheight = 2;
  59.         gridBag.setConstraints(output, tc);
  60.         add(output);
  61.  
  62.         GridBagConstraints lc = new GridBagConstraints();
  63.         lc.fill = GridBagConstraints.VERTICAL;
  64.         lc.gridwidth = GridBagConstraints.REMAINDER; //end row
  65.         gridBag.setConstraints(spanish, lc);
  66.         add(spanish);
  67.         gridBag.setConstraints(italian, lc);
  68.         add(italian);
  69.  
  70.         validate();
  71.     }
  72.  
  73.     public boolean action(Event e, Object arg) {
  74.         if (e.target instanceof List) {
  75.             String language = (e.target == spanish) ?
  76.                               "Spanish" : "Italian";
  77.             output.appendText("Action event occurred on \""
  78.                               + (String)arg  + "\" in "
  79.                               + language + ".\n");
  80.     }
  81.     return true;
  82.     }
  83.  
  84.     public boolean handleEvent(Event e) {
  85.         if (e.target instanceof List) {
  86.             List list = (List)(e.target);
  87.             String language = (list == spanish) ?
  88.                               "Spanish" : "Italian";
  89.  
  90.             switch (e.id) {
  91.               case Event.LIST_SELECT:
  92.                 int sIndex = ((Integer)e.arg).intValue();
  93.                 output.appendText("Select event occurred on item #"
  94.                                   + sIndex + " (\""
  95.                                   + list.getItem(sIndex)  + "\") in "
  96.                               + language + ".\n");
  97.                 break;
  98.               case Event.LIST_DESELECT:
  99.                 int dIndex = ((Integer)e.arg).intValue();
  100.                 output.appendText("Deselect event occurred on item #"
  101.                                   + dIndex + " (\""
  102.                                   + list.getItem(dIndex)  + "\") in "
  103.                               + language + ".\n");
  104.             }
  105.         }
  106.         return super.handleEvent(e);
  107.     }
  108. }
  109.