Javatpoint Logo
Javatpoint Logo

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

AWT List Class Constructors

Sr. no. Constructor Description
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 multipleMode) It constructs a new scrolling list initialized which displays the given number of rows.

Methods Inherited by the List Class

The List class methods are inherited by following classes:

  • java.awt.Component
  • java.lang.Object

List Class Methods

Sr. no. Method name Description
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.
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.
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 processActionEvent(ActionEvent e) It process the action events occurring on list by 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 e) It process the item events occurring on list by dispatching them to a registered ItemListener object.
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.

ListExample1.java

Output:

java awt list example 1

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

Output:

java awt list example 2
Next TopicJava AWT Canvas





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA