Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

Java AWT List

Uploaded by

Tejas Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Java AWT List

Uploaded by

Tejas Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Dr Bharti Sharma

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.

AWT List class Declaration

1. public class List extends Component implements ItemSelectable, Accessible

AWT List Class Constructors

Sr. Constructor Description


no.

1. List() It constructs a new scrolling list.


2. List(int row_num) It constructs a new scrolling list initialized with the given number of rows
visible.
3. List(int row_num, Boolean It constructs a new scrolling list initialized which displays the given
multipleMode) number of rows.

Methods Inherited by the List Class

The List class methods are inherited by following classes:


Dr Bharti Sharma

o java.awt.Component
o java.lang.Object

List Class Methods

Sr. Method name Description


no.

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

8. ActionListener[] getActionListeners() It returns an array of action listeners registered on the


list.
9. String getItem(int index) It fetches the item related to given index position.
10. int getItemCount() It gets the count/number of items in the list.
11. ItemListener[] getItemListeners() It returns an array of item listeners registered on the list.
12. String[] getItems() It fetched the items from the list.
13. Dimension getMinimumSize() It gets the minimum size of a scrolling list.
14. Dimension getMinimumSize(int rows) It gets the minimum size of a list with given number of
rows.
15. Dimension getPreferredSize() It gets the preferred size of list.
16. Dimension getPreferredSize(int rows) It gets the preferred size of list with given number of
rows.
17. int getRows() It fetches the count of visible rows in the list.
18. int getSelectedIndex() It fetches the index of selected item of list.
19. int[] getSelectedIndexes() It gets the selected indices of the list.
Dr Bharti Sharma

20. String getSelectedItem() It gets the selected item on the list.


21. String[] getSelectedItems() It gets the selected items on the list.
22. Object[] getSelectedObjects() It gets the selected items on scrolling list in array of
objects.
23. int getVisibleIndex() It gets the index of an item which was made visible by
method makeVisible()
24. void makeVisible(int index) It makes the item at given index visible.
25. boolean isIndexSelected(int index) It returns true if given item in the list is selected.
26. boolean isMultipleMode() It returns the true if list allows multiple selections.
27. protected String paramString() It returns parameter string representing state of the
scrolling list.
28. protected void It process the action events occurring on list by
processActionEvent(ActionEvent e) dispatching them to a registered ActionListener object.
29. protected void processEvent(AWTEvent e) It process the events on scrolling list.
30. protected void processItemEvent(ItemEvent It process the item events occurring on list by
e) dispatching them to a registered ItemListener object.
Dr Bharti Sharma

31. void removeActionListener(ActionListener l) It removes specified action listener. Thus it doesn't


receive further action events from the list.
32. void removeItemListener(ItemListener l) It removes specified item listener. Thus it doesn't
receive further action events from the list.
33. void remove(int position) It removes the item at given index position from the list.
34. void remove(String item) It removes the first occurrence of an item from list.
35. void removeAll() It removes all the items from the list.
36. void replaceItem(String newVal, int index) It replaces the item at the given index in list with the
new string specified.
37. void select(int index) It selects the item at given index in the list.
38. void setMultipleMode(boolean b) It sets the flag which determines whether the list will
allow multiple selection or not.
39. void removeNotify() It removes the peer of list.

Java AWT List Example

In the following example, we are creating a List component with 5 rows and adding it into the Frame.
Dr Bharti Sharma

ListExample1.java

1. // importing awt class


2. import java.awt.*;
3.
4. public class ListExample1
5. {
6. // class constructor
7. ListExample1() {
8. // creating the frame
9. Frame f = new Frame();
10. // creating the list of 5 rows
11. List l1 = new List(5);
12.
13. // setting the position of list component
14. l1.setBounds(100, 100, 75, 75);
15.
16. // adding list items into the list
17. l1.add("Item 1");
18. l1.add("Item 2");
19. l1.add("Item 3");
20. l1.add("Item 4");
21. l1.add("Item 5");
22.
Dr Bharti Sharma

23. // adding the list to frame


24. f.add(l1);
25.
26. // setting size, layout and visibility of frame
27. f.setSize(400, 400);
28. f.setLayout(null);
29. f.setVisible(true);
30. }
31.
32. // main method
33. public static void main(String args[])
34. {
35. new ListExample1();
36. }
37. }

Output:
Dr Bharti Sharma

Java AWT List Example with ActionListener

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

1. // importing awt and event class


2. import java.awt.*;
3. import java.awt.event.*;
4.
5. public class ListExample2
Dr Bharti Sharma

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

54. b.addActionListener(new ActionListener() {


55. public void actionPerformed(ActionEvent e) {
56. String data = "Programming language Selected: "+l1.getItem(l1.getSelectedIndex());
57. data += ", Framework Selected:";
58. for(String frame:l2.getSelectedItems()) {
59. data += frame + " ";
60. }
61. label.setText(data);
62. }
63. });
64. }
65.
66. // main method
67. public static void main(String args[])
68. {
69. new ListExample2();
70. }
71. }

Output:
Dr Bharti Sharma

You might also like