home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / test / listtes1.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  3.5 KB  |  140 lines

  1. /*
  2.  * @(#)ListTest2.java    1.2 95/09/01 Sami Shaio
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. import java.awt.*;
  20.  
  21. /**
  22.  * A test of a Container with BorderLayout.
  23.  */
  24. public class ListTest2 extends Frame {
  25.     List list;
  26.     TextField text;
  27.  
  28.     public ListTest2() {
  29.     super("ListTest2");
  30.  
  31.     list = new List();
  32.     list.addItem("one");
  33.     list.addItem("two");
  34.     list.addItem("three");
  35.     list.addItem("four");
  36.     list.addItem("five");
  37.     list.addItem("six");
  38.     list.addItem("seven");
  39.     list.addItem("eight");
  40.     list.addItem("nine");
  41.     list.addItem("ten");
  42.     add("Center", list);
  43.  
  44.     Panel p = new Panel();
  45.     p.add(text=new TextField(20));
  46.     p.add(new Checkbox("multiple"));
  47.     p.add(new Button("add"));
  48.     p.add(new Button("del"));
  49.     p.add(new Button("clear"));
  50.     p.add(new Button("select"));
  51.     p.add(new Button("deselect"));
  52.     p.add(new Button("print"));
  53.     add("South", p);
  54.  
  55.     move(200, 100);
  56.     resize(300, 300);
  57.     show();
  58.     }
  59.  
  60.     public boolean handleEvent(Event evt) {
  61.     switch (evt.id) {
  62.       case Event.LIST_SELECT:
  63.         System.out.println("List select: " + ((Integer)evt.arg).intValue());
  64.         return true;
  65.       case Event.LIST_DESELECT:
  66.         System.out.println("List deselect: " + ((Integer)evt.arg).intValue());
  67.         return true;
  68.       case Event.WINDOW_DESTROY:
  69.         System.exit(0);
  70.         return true;
  71.       default:
  72.         return super.handleEvent(evt);
  73.     }
  74.     }
  75.  
  76.     int argToIndex(String arg) {
  77.     int n = list.countItems();
  78.  
  79.     for (int i = 0; i < n; i++) {
  80.         if (arg.equals(list.getItem(i))) {
  81.         return i;
  82.         }
  83.     }
  84.     return -1;
  85.     }
  86.  
  87.     public boolean action(Event evt, Object arg) {
  88.     if (evt.id == Event.ACTION_EVENT) {
  89.         if (evt.target instanceof Checkbox) {
  90.         list.setMultipleSelections(((Boolean)arg).booleanValue());
  91.         } else if ("print".equals(evt.arg)) {
  92.         System.out.println("-- list contents --");
  93.         int n = list.countItems();
  94.         for (int i=0; i < n; i++) {
  95.             System.out.println(list.getItem(i));
  96.         }
  97.         System.out.println("-- list selected --");        
  98.         String sel[] = list.getSelectedItems();
  99.         for (int i = 0 ; i < sel.length ; i++) {
  100.             System.out.println(sel[i]);
  101.         }
  102.         } else if ("add".equals(evt.arg)) {
  103.         list.addItem(text.getText());
  104.         } else if ("del".equals(evt.arg)) {
  105.         int index = argToIndex(text.getText());
  106.  
  107.         if (index == -1) {
  108.             System.out.println("Invalid index");
  109.         } else {
  110.             list.delItem(index);
  111.         }
  112.         } else if ("select".equals(evt.arg)) {
  113.         int index = argToIndex(text.getText());
  114.  
  115.         if (index == -1) {
  116.             System.out.println("Invalid index");
  117.         } else {
  118.             list.select(index);
  119.         }
  120.         } else if ("deselect".equals(evt.arg)) {
  121.         int index = argToIndex(text.getText());
  122.  
  123.         if (index == -1) {
  124.             System.out.println("Invalid index");
  125.         } else {
  126.             list.deselect(index);
  127.         }
  128.         } else if ("clear".equals(evt.arg)) {
  129.         list.clear();
  130.         }
  131.     }
  132.  
  133.     return true;
  134.     }
  135.  
  136.     public static void main(String args[]) {
  137.     new ListTest2();
  138.     }
  139. }
  140.