Java
  Home arrow Java arrow Page 2 - Java's Advanced User Interface Compone...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java's Advanced User Interface Components
By: Gayathri Gokul
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2003-11-11

    Table of Contents:
  • Java's Advanced User Interface Components
  • More on the List Class
  • Scrollbars and Sliders
  • Canvases

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Java's Advanced User Interface Components - More on the List Class


    (Page 2 of 4 )

    The following example is a simple applet that creates a scrolling list and populates it with items, the list is added to the container in this case Applet using the add() method. In the example below we have a list of seven dairy items with multi-select option set to true.

    /* <Applet code “ScrollListTest.class”
    Width = 200
    Height = 200>
    </applet>
    */

    Import java.awt.*;

    Public class ScrollListTest extends java.applet.Applet
    {
     List Dairy = new List (7,true)

     Public void init ()
     {
      Diary.addItem (“Whole Milk”);
      Diary.addItem (“Skimmed Milk”);
      Diary.addItem (“Butter”);
      Diary.addItem (“Cheese”);
      Diary.addItem (“Yogurt”);
      Diary.addItem (“Ice Cream”);
      Diary.addItem (“ Butter Milk ”);
     }
    }


    Scrolling lists generate actions when the user double-clicks a list item, for example a single mouse click generates a LIST_SELECT or LIST_DESELECT event ID. A scrolling list action has the argument of the string of the item that was double-clicked. The following table shows some of the methods available to scrolling lists. See the API documentation for a complete set.

    Method Action
    getItem(int)

    Returns the string item at the given position (items inside a list begin at 0, just like arrays)

    countItems()

    Returns the number of items in the menu

    getSelectedIndex()

    Returns the index position of the item that's selected

    getSelectedIndexes()

    Returns an array of index positions (used for lists that allow multiple selections)

    getSelectedItem()

    Returns the currently selected item as a string

    getSelectedItems()

    Returns an array of strings containing all the selected items

    select(int)

    Selects the item at the given position

    select(String)

    Selects the item with the given string



    The following example creates a slightly complex Applet which contains the following UI components: A TextField to accept user input; a Label, to inform the user of the contents that is to be entered in the TextField; a List, which displays a number of items; a “but_add” button, which let’s the user to add text in the TextField to the List. When the applet is started, the UI components specified are added to the applet. The addItem() method is used to add the items to the list. The text entered in the TextField is added to the List on clicking the add button. The ActionListener() method enables the program to listen to the button click events. The items are added to the list inside the actionPerformed() method.

    /*
    &lgt;applet code = "ListTest1.class" height = 300 width = 200>
    &lgt;/applet>
    */

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;

    Public class ListTest1 extends Applet
    {
     List acts = new List();
     TextField tx = new TextField(10);
     Button but_add = new Button("ADD");
     String stringlist[] = {"One", "Two", "Three", “Four”};

     Public void start ()
     {
      add(new Label("Text"));
      add(tx);
      for(int i = 0; i < stringlist.length; ++i)
      acts.addItem(stringlist[i]);
      add(acts);
      but_add.addActionListener(new AddLisn());
      add(but_add);
     }
    Class AddLisn implements ActionListener
    {
     public void actionPerformed(ActionEvent e)
     {
     acts.addItem(tx.getText());
     }
    }

    }


    More Java Articles
    More By Gayathri Gokul


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway