Advertisement
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 | 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. |
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();
}
}
Advertisement
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();
}
}
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 -:
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 -:
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement