Java AWT List Class Declaration

The List Class in Java extends the Component class and also implements the ItemSelectable and Accessible intergaves. This allows to representation of a selectable list of some options in UI form and also the feature for accessing the options.

Syntax:

public class List extends Component implements ItemSelectable, Accessible  

Constructors of List Class

The table provides the constructors present in the List along with their descriptions.

Constructors

Description

List()

Used to create an empty list with no visible items.

List(int numRows)

Used to create an empty list for the given number of visible rows.

List(int numRows, boolean multipleMode)

Used to create an empty list for the given number of visible rows and set whether multiple selections are allowed (multipleMode).

Methods of List class are inherited by two classes:

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

Methods of List Class

The table provides the Methods present in the List along with its description.

Method

Description

void add(String item)

Adds the specified item to the end of the list.

void add(String item, int index)

Used to insert the given item to the given index in the list.

void addActionListener(ActionListener l)

Registers an ActionListener to listen for item selection events in the list.

void addItemListener(ItemListener l)

Registers an ItemListener to listen for item selection events in the list.

void addNotify()

Creates the peer of the list, if it doesn’t already exist.

void deselect(int index)

Deselects the item at the specified index in a multiple-selection list.

AccessibleContext getAccessibleContext()

Gets the AccessibleContext associated with this List.

ActionListener[] getActionListeners()

Used to return an array of all the ActionListeners added to this list.

String getItem(int index)

Retrieves the item at the specified index in the list.

int getItemCount()

Returns length(the number of items) in the list.

ItemListener[] getItemListeners()

Used to return an array of all the ItemListeners added to this list.

String[] getItems()

Used to return an array of all the items in the list.

Dimension getMinimumSize()

Used to return the minimum size of the list.

Dimension getMinimumSize(int rows)

Used to return the minimum size needed to display the specified number of rows.

Dimension getPreferredSize()

Used to return the preferred size of the list.

Dimension getPreferredSize(int rows)

Used to return the preferred size needed to display the specified number of rows.

int getRows()

Used to return the number of rows present in the list.

int getSelectedIndex()

Returns the index of the first selected item in a multiple-selection list.

int[] getSelectedIndexes()

Returns an array of the selected item indexes in a multiple-selection list.

String getSelectedItem()

Returns the selected item in a single-selection list.

String[] getSelectedItems()

Returns an array of the selected item values in a multiple-selection list.

Object[] getSelectedObjects()

Returns an array of selected items in a multiple-selection list.

int getVisibleIndex()

Returns the index of the first visible item in the list.

void makeVisible(int index)

Scrolls the list to make the item at the given index visible.

boolean isIndexSelected(int index)

Checks if the item at the given index is selected in a multiple-selection list.

boolean isMultipleMode()

Check if the list allows multiple selections.

protected String paramString()

Returns a string representing the state of this list.

protected void processActionEvent(ActionEvent e)

Processes action events occurring in this list.

protected void processEvent(AWTEvent e)

Processes events occurring in this list.

protected void processItemEvent(ItemEvent e)

Processes item events occurring in this list.

void removeActionListener(ActionListener l)

Used to remove an ActionListener from the list.

void removeItemListener(ItemListener l)

Used to remove an ItemListener from the list.

void remove(int position)

Used to remove the item at the given index from the list.

void remove(String item)

Used to remove the first occurrence of the given item from the list.

void removeAll()

Used to remove all items from the list.

void replaceItem(String newVal, int index)

Replace the item at the given index with a new item.

void select(int index)

Selects the item at the given index in the list.

void setMultipleMode(boolean b)

Sets whether the list allows multiple selections (true) or single selection (false).

void removeNotify()

Used to destroys the peer of the list.

Java AWT List

AWT or Abstract Window Toolkit is a set of various UI components with the List Component. We can display a scrollable list of items through which users can select one or multiple options. AWT List is embedded into the AWT library which is used to build a GUI to present a set of choices to the user. We can customize this by adding or removing the items dynamically from the list. In this article, we will the Declaration of AWT List, Constructors, Methods, and some practical examples for creating LIst with various logics.

Similar Reads

Java AWT List Class Declaration

The List Class in Java extends the Component class and also implements the ItemSelectable and Accessible intergaves. This allows to representation of a selectable list of some options in UI form and also the feature for accessing the options....

Example of Java AWT List

Example 1:...

Contact Us