Java AWT List
Java AWT List
The object of List class represents a list of text items. With the help of the List class, user can choose either
one item or multiple items. It inherits the Component class.
o java.awt.Component
o java.lang.Object
1. void add(String item) It adds the specified item into the end of scrolling list.
2. void add(String item, int index) It adds the specified item into list at the given index
position.
3. void addActionListener(ActionListener l) It adds the specified action listener to receive action
events from list.
4. void addItemListener(ItemListener l) It adds specified item listener to receive item events
from list.
5. void addNotify() It creates peer of list.
6. void deselect(int index) It deselects the item at given index position.
7. AccessibleContext getAccessibleContext() It fetches the accessible context related to the list.
Dr Bharti Sharma
In the following example, we are creating a List component with 5 rows and adding it into the Frame.
Dr Bharti Sharma
ListExample1.java
Output:
Dr Bharti Sharma
In the following example, we are creating two List components, a Button and a Label and adding them into
the frame. Here, we are generating an event on the button using the addActionListener(ActionListener l)
method. On clicking the button, it displays the selected programming language and the framework.
ListExample2.java
6. {
7. // class constructor
8. ListExample2() {
9. // creating the frame
10. Frame f = new Frame();
11. // creating the label which is final
12. final Label label = new Label();
13.
14. // setting alignment and size of label
15. label.setAlignment(Label.CENTER);
16. label.setSize(500, 100);
17.
18. // creating a button
19. Button b = new Button("Show");
20.
21. // setting location of button
22. b.setBounds(200, 150, 80, 30);
23.
24. // creating the 2 list objects of 4 rows
25. // adding items to the list using add()
26. // setting location of list components
27. final List l1 = new List(4, false);
28. l1.setBounds(100, 100, 70, 70);
29. l1.add("C");
Dr Bharti Sharma
30. l1.add("C++");
31. l1.add("Java");
32. l1.add("PHP");
33.
34.
35. final List l2=new List(4, true);
36. l2.setBounds(100, 200, 70, 70);
37. l2.add("Turbo C++");
38. l2.add("Spring");
39. l2.add("Hibernate");
40. l2.add("CodeIgniter");
41.
42. // adding List, Label and Button to the frame
43. f.add(l1);
44. f.add(l2);
45. f.add(label);
46. f.add(b);
47.
48. // setting size, layout and visibility of frame
49. f.setSize(450,450);
50. f.setLayout(null);
51. f.setVisible(true);
52.
53. // generating event on the button
Dr Bharti Sharma
Output:
Dr Bharti Sharma