Java AWT List


The object of List class represents a list of text items. By the help of list, user can choose either one item or multiple items. It inherits Component class.

AWT List class Declaration

  1. public class List extends Component implements ItemSelectable, Accessible  

Java AWT List Example

  1. import java.awt.*;  
  2. public class ListExample  
  3. {  
  4.      ListExample(){  
  5.         Frame f= new Frame();  
  6.         List l1=new List(5);  
  7.         l1.setBounds(100,10075,75);  
  8.         l1.add("Item 1");  
  9.         l1.add("Item 2");  
  10.         l1.add("Item 3");  
  11.         l1.add("Item 4");  
  12.         l1.add("Item 5");  
  13.         f.add(l1);  
  14.         f.setSize(400,400);  
  15.         f.setLayout(null);  
  16.         f.setVisible(true);  
  17.      }  
  18. public static void main(String args[])  
  19. {  
  20.    new ListExample();  
  21. }  
  22. }  

Output:

java awt list example 1

Java AWT List Example with ActionListener

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class ListExample  
  4. {  
  5.      ListExample(){  
  6.         Frame f= new Frame();  
  7.         final Label label = new Label();          
  8.         label.setAlignment(Label.CENTER);  
  9.         label.setSize(500,100);  
  10.         Button b=new Button("Show");  
  11.         b.setBounds(200,150,80,30);  
  12.         final List l1=new List(4false);  
  13.         l1.setBounds(100,10070,70);  
  14.         l1.add("C");  
  15.         l1.add("C++");  
  16.         l1.add("Java");  
  17.         l1.add("PHP");  
  18.         final List l2=new List(4true);  
  19.         l2.setBounds(100,20070,70);  
  20.         l2.add("Turbo C++");  
  21.         l2.add("Spring");  
  22.         l2.add("Hibernate");  
  23.         l2.add("CodeIgniter");  
  24.         f.add(l1); f.add(l2); f.add(label); f.add(b);  
  25.         f.setSize(450,450);  
  26.         f.setLayout(null);  
  27.         f.setVisible(true);  
  28.         b.addActionListener(new ActionListener() {  
  29.          public void actionPerformed(ActionEvent e) {       
  30.           String data = "Programming language Selected: "+l1.getItem(l1.getSelectedIndex());  
  31.           data += ", Framework Selected:";  
  32.           for(String frame:l2.getSelectedItems()){  
  33.                    data += frame + " ";  
  34.           }  
  35.           label.setText(data);  
  36.           }  
  37.          });          
  38. }  
  39. public static void main(String args[])  
  40. {  
  41.    new ListExample();  
  42. }  
  43. }  

Output:

java awt list example 2





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition