R4R
Right Place For Right Person TM
 

R4R JAVAAWT Examples Handling List in java

previous

Home

Next

Handling List  in java

In this example we are going to handle List.
To handle the List we are using three type of package. One is java.awt.* second is  java.applet.* and third is java.awt.event.*; .The java.awt.* is used for GUI interface. AWT classes are contain in this package. The  java.applet.* package is used to create an applet and java.awt.event.*; defined within the java.awt.*; package is a subclass of EventObject. It is a super class of all AWT-based event.The list provides a compact ,multiple choice and scrolling selection list.

List provide these contractures:
List(): this allowed to select only one item at any one time. 
List(int numRows): the num Rows specifies the number of entries in the list that will always visible .
List(int numRows, boolean multiple Select): if multiple Select is true then we will select two or more item at a time.
To add a selection to the list, call add().
void add(string name);
add item to the end of list.
void add(string name,int index); add item to the index.
In our example first we implement two interface:
ActionListener();  each listener implement the ActionLinstner.
actionPerformed();
When a option is selected or an event is occur this interface is called.
To deterrmine which item is currently selected, you may call :
String getSelectedItem();
It returns a string containing the name of the item.
or  int getSelectedIndex(); It return the index of the item.
To obtain the number of item in the list call:
 int getItemCount();
In our example we create two List one is  country and other is city.



// Handling List 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="ListDemo" width=300 height=200>
</applet>
*/
public class ListDemo extends Applet implements ActionListener{
List country,city;
String msg="";
public void init(){
 country = new List(3,false);
 city = new List(3,false);
country.add("India");
country.add("Rusia");
country.add("Pakistan");
country.add("Srilanka");
city.add("unnao");
city.add("Lucknow");
city.add("Lahour"); 
city.add("Kuvait");
add(country);
add(city);
country.addActionListener(this);
city.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
repaint();
}
public void paint(Graphics g){
int idx[];
msg="your country name is:";
idx=country.getSelectedIndexes();
for(int i=0;i<idx.length;i++)
msg +=country.getItem(idx[i]) +" ";
g.drawString(msg,6,120);
msg="your city name is:";
msg +=city.getSelectedItem();
g.drawString(msg,6,140);
}
}

Download Source Code

Output Of Example

previous

Home

Next

New Updates

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R