< Prev
Next >



List class





List class is used to create a list with multiple values, allowing a user to select any of the values. When a value is selected from List, an ItemEvent is generated, which is handled by implementing ItemListener interface. List is another component in AWT which extends Component class.





Constructors of List


Constructor Description
public List() Creates a scrolling list.
public List(int rows) Creates a List with a number of rows.
public List(int rows, boolean mode) Creates a List with a number of rows, allowing us to select multiple values from list if mode is true.





Methods of List class


Methods Description
public void add(String item) Adds the item to the end of List.
public void add(String item, int index) Adds the item at a specific index in the list.
public void addActionListener(ActionListener al) Adds a specific ActionListener to listen an Action event generated when item is selected from this list.
public void addItemListener(ItemListener al) Adds a specific ItemListener to listen an Item event generated when item is selected from this list.
public String getItem( int index) Gets an item from a specific index in the list.
public String getSelectedItem() Gets the selected item in the list.
public String[] getSelectedItems() Gets the selected item in the list.
public int getRows() Gets the total number of rows in the list.





An example of List, where you can select one item from the list.


We are going to create a list of sports with 7 items, you can only select one item from the list.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ListEx1 
{
String [] seasons;
Frame jf;
List list;
Label label1;

ListEx1()
{
jf= new Frame("List");
list= new List(7);
label1 = new Label("Select your favorite sports from the list :");

list.add("Badminton");
list.add("Hockey");
list.add("Tennis");
list.add("Football");
list.add("Cricket");
list.add("Formula One");
list.add("Rugby");

jf.add(label1);
jf.add(list);

jf.setLayout(new FlowLayout());
jf.setSize(260,220);
jf.setVisible(true);
}

public static void main(String... ar)
{
new ListEx1();
}

}

When you run the code, you are presented a window displaying a list of sports, asking you to select your favorite one from the list. We have selected Tennis from the list. -:

Figure 1





An example of List, where you can select multiple items from the list.


We are going to create a list of seasons with 5 items, you can only multiple items from the list.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ListEx2
{
String [] seasons;
Frame jf;
List list;
Label label1;


ListEx2()
{
 
jf= new Frame("List");

//Creating a list with 5 items and boolean true allows us to select multiple items in the list.
list= new List(5, true);

label1 = new Label("Select your favorite seasons from the list :");

list.add("Spring");
list.add("Summer");
list.add("Monsoon");
list.add("Autumn");
list.add("Winter");

jf.add(label1);
jf.add(list);

jf.setLayout(new FlowLayout());
jf.setSize(260,200);
jf.setVisible(true);
}


public static void main(String... ar)
{
new ListEx2();
}

}

When you run the code, you are presented a window displaying a list of seasons, asking you to select your favorite seasons from the list. We have selected Spring and Monsoon from the list. -:

Figure 2





Handling List events when one item can be selected in the list


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;




public class ListEx4 implements ItemListener
{
Frame jf;
List list;
Label label1, label2;

ListEx4()
{
jf= new Frame("List");
list= new List(8);
list.add("Red");
list.add("Black");
list.add("Blue");
list.add("Yellow");
list.add("Green");
list.add("Gray");
list.add("Turquoise");
list.add("Purple");

label1 = new Label("Please select your favorite color", Label.CENTER);
label2 = new Label();

jf.add(label1);
jf.add(list);
jf.add(label2);

//Registering class ListEx3 to listen to an ItemEvent, generated when an item in the list is selected.
list.addItemListener(this);

jf.setLayout(new FlowLayout());
jf.setSize(280,240);
jf.setVisible(true);
}


public void itemStateChanged(ItemEvent e)
{
List l = (List)e.getSource();
label2.setText("You have selected "+ l.getSelectedItem());
jf.setVisible(true);
}

public static void main(String... ar)
{
new ListEx4();
}

}
When you run the code, you are presented a window shown in the Figure3. In this window you are asked to select one of colors from the list -:

Figure 3


We have selected item Blue from the list and hence this selected item is notified to us, shown in the figure below
Figure4





Handling List events when multiple items can be selected in the list.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class ListEx4 implements ItemListener
{
Frame jf;
List list;
Label label1, label2;

ListEx5()
{
jf= new Frame("List");
list= new List(8,true);
list.add("Red");
list.add("Black");
list.add("Blue");
list.add("Yellow");
list.add("Green");
list.add("Gray");
list.add("Turquoise");
list.add("Purple");

label1 = new Label("Please select your favorite color", Label.CENTER);
label2 = new Label();

jf.add(label1);
jf.add(list);
jf.add(label2);

//Registering class ListEx4 to listen to an ItemEvent, generated when an item in the list is selected.
list.addItemListener(this);

jf.setLayout(new FlowLayout());
jf.setSize(280,240);
jf.setVisible(true);
}


public void itemStateChanged(ItemEvent e)
{
String selected="";
List l = (List)e.getSource();
String str[]= l.getSelectedItems();

for(String s: str)
selected += s + " ";

label2.setText("You have selected - "+ selected);
jf.setVisible(true);
}

public static void main(String... ar)
{
new ListEx4();
}

}
When you run the code, you are presented a window shown in the Figure5. In this window you are asked to select one of colors from the list -:

Figure 5


We have selected item Blue and Turquoise from the list and hence these multiple selected items are notified to us, shown in the figure below

Figure6




Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< CheckBox
ScrollBar >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2