Home arrow Java & J2EE arrow Page 2 - Java's Advanced User Interface Components

More on the List Class - Java

javaLast time, we mastered some basic UI components and worked on how to add them to panels, organize their layout, and manage their events. Having covered all the basic techniques that we’ll need to get started with Java interface programming in our first article, Java's Basic User Interface Components, we will move on to some advanced UI components. Today, I’ll be covering scrolling lists, scrollbars, and canvases, and their functionality. It is amazing to see how many of complex tasks are simplified with the help of these advanced user interface components.

TABLE OF CONTENTS:
  1. Java's Advanced User Interface Components
  2. More on the List Class
  3. Scrollbars and Sliders
  4. Canvases
By: Gayathri Gokul
Rating: starstarstarstarstar / 18
November 11, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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 & J2EE Articles          >>> More By Gayathri Gokul
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: