Get Item From AWT List Example
- /*
- Get Item From AWT List Example
- This java example shows how to get item from a list using Java AWT
- List class.
- */
- import java.applet.Applet;
- import java.awt.Graphics;
- import java.awt.List;
- /*
- <applet code="GetItemExample" width=200 height=200>
- </applet>
- */
- public class GetItemExample extends Applet{
- List list = null;
- public void init(){
- //create a list
- list = new List(5, true);
- //add items to a list
- list.add("One");
- list.add("Two");
- list.add("Three");
- list.add("Four");
- list.add("Five");
- list.add("Six");
- list.add("Seven");
- //add list
- add(list);
- }
- public void paint(Graphics g){
- /*
- * To get an item from a list, use
- * String getItem(int index)
- * method.
- */
- g.drawString("Item at index 2 is: " +list.getItem(2),10,120);
- }
- }
Example Output




