Class List
- All Implemented Interfaces:
-
ImageObserver
,ItemSelectable
,MenuContainer
,Serializable
,Accessible
public class List extends Component implements ItemSelectable, Accessible
List
component presents the user with a scrolling list of text items. The list can be set up so that the user can choose either one item or multiple items. For example, the code . . .
List lst = new List(4, false); lst.add("Mercury"); lst.add("Venus"); lst.add("Earth"); lst.add("JavaSoft"); lst.add("Mars"); lst.add("Jupiter"); lst.add("Saturn"); lst.add("Uranus"); lst.add("Neptune"); lst.add("Pluto"); cnt.add(lst);
where cnt
is a container, produces the following scrolling list:
If the List allows multiple selections, then clicking on an item that is already selected deselects it. In the preceding example, only one item from the scrolling list can be selected at a time, since the second argument when creating the new scrolling list is false
. If the List does not allow multiple selections, selecting an item causes any other selected item to be deselected.
Note that the list in the example shown was created with four visible rows. Once the list has been created, the number of visible rows cannot be changed. A default List
is created with four rows, so that lst = new List()
is equivalent to list = new List(4, false)
.
Beginning with Java 1.1, the Abstract Window Toolkit sends the List
object all mouse, keyboard, and focus events that occur over it. (The old AWT event model is being maintained only for backwards compatibility, and its use is discouraged.)
When an item is selected or deselected by the user, AWT sends an instance of ItemEvent
to the list. When the user double-clicks on an item in a scrolling list, AWT sends an instance of ActionEvent
to the list following the item event. AWT also generates an action event when the user presses the return key while an item in the list is selected.
If an application wants to perform some action based on an item in this list being selected or activated by the user, it should implement ItemListener
or ActionListener
as appropriate and register the new listener to receive events from this list.
For multiple-selection scrolling lists, it is considered a better user interface to use an external gesture (such as clicking on a button) to trigger the action.
- Since:
- 1.0
- See Also:
Nested Class Summary
Modifier and Type | Class | Description |
---|---|---|
protected class |
List.AccessibleAWTList |
This class implements accessibility support for the List class. |
Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
Field Summary
Fields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor Summary
Constructor | Description |
---|---|
List() |
Creates a new scrolling list. |
List |
Creates a new scrolling list initialized with the specified number of visible lines. |
List |
Creates a new scrolling list initialized to display the specified number of rows. |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
void |
add |
Adds the specified item to the end of scrolling list. |
void |
add |
Adds the specified item to the scrolling list at the position indicated by the index. |
void |
addActionListener |
Adds the specified action listener to receive action events from this list. |
void |
addItem |
Deprecated. |
void |
addItem |
Deprecated. replaced by add(String, int) . |
void |
addItemListener |
Adds the specified item listener to receive item events from this list. |
void |
addNotify() |
Creates the peer for the list. |
boolean |
allowsMultipleSelections() |
Deprecated. As of JDK version 1.1, replaced by isMultipleMode() . |
void |
clear() |
Deprecated. As of JDK version 1.1, replaced by removeAll() . |
int |
countItems() |
Deprecated. As of JDK version 1.1, replaced by getItemCount() . |
void |
delItem |
Deprecated. replaced by remove(String) and remove(int) . |
void |
delItems |
Deprecated. As of JDK version 1.1, Not for public use in the future. |
void |
deselect |
Deselects the item at the specified index. |
AccessibleContext |
getAccessibleContext() |
Gets the AccessibleContext associated with this List . |
ActionListener[] |
getActionListeners() |
Returns an array of all the action listeners registered on this list. |
String |
getItem |
Gets the item associated with the specified index. |
int |
getItemCount() |
Gets the number of items in the list. |
ItemListener[] |
getItemListeners() |
Returns an array of all the item listeners registered on this list. |
String[] |
getItems() |
Gets the items in the list. |
<T extends EventListener> |
getListeners |
Returns an array of all the objects currently registered as FooListener s upon this List . |
Dimension |
getMinimumSize() |
Determines the minimum size of this scrolling list. |
Dimension |
getMinimumSize |
Gets the minimum dimensions for a list with the specified number of rows. |
Dimension |
getPreferredSize() |
Gets the preferred size of this scrolling list. |
Dimension |
getPreferredSize |
Gets the preferred dimensions for a list with the specified number of rows. |
int |
getRows() |
Gets the number of visible lines in this list. |
int |
getSelectedIndex() |
Gets the index of the selected item on the list, |
int[] |
getSelectedIndexes() |
Gets the selected indexes on the list. |
String |
getSelectedItem() |
Gets the selected item on this scrolling list. |
String[] |
getSelectedItems() |
Gets the selected items on this scrolling list. |
Object[] |
getSelectedObjects() |
Gets the selected items on this scrolling list in an array of Objects. |
int |
getVisibleIndex() |
Gets the index of the item that was last made visible by the method makeVisible . |
boolean |
isIndexSelected |
Determines if the specified item in this scrolling list is selected. |
boolean |
isMultipleMode() |
Determines whether this list allows multiple selections. |
boolean |
isSelected |
Deprecated. As of JDK version 1.1, replaced by isIndexSelected(int) . |
void |
makeVisible |
Makes the item at the specified index visible. |
Dimension |
minimumSize() |
Deprecated. As of JDK version 1.1, replaced by getMinimumSize() . |
Dimension |
minimumSize |
Deprecated. As of JDK version 1.1, replaced by getMinimumSize(int) . |
protected String |
paramString() |
Returns the parameter string representing the state of this scrolling list. |
Dimension |
preferredSize() |
Deprecated. As of JDK version 1.1, replaced by getPreferredSize() . |
Dimension |
preferredSize |
Deprecated. As of JDK version 1.1, replaced by getPreferredSize(int) . |
protected void |
processActionEvent |
Processes action events occurring on this component by dispatching them to any registered ActionListener objects. |
protected void |
processEvent |
Processes events on this scrolling list. |
protected void |
processItemEvent |
Processes item events occurring on this list by dispatching them to any registered ItemListener objects. |
void |
remove |
Removes the item at the specified position from this scrolling list. |
void |
remove |
Removes the first occurrence of an item from the list. |
void |
removeActionListener |
Removes the specified action listener so that it no longer receives action events from this list. |
void |
removeAll() |
Removes all items from this list. |
void |
removeItemListener |
Removes the specified item listener so that it no longer receives item events from this list. |
void |
removeNotify() |
Removes the peer for this list. |
void |
replaceItem |
Replaces the item at the specified index in the scrolling list with the new string. |
void |
select |
Selects the item at the specified index in the scrolling list. |
void |
setMultipleMode |
Sets the flag that determines whether this list allows multiple selections. |
void |
setMultipleSelections |
Deprecated. As of JDK version 1.1, replaced by setMultipleMode(boolean) . |
Methods declared in class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, postEvent, prepareImage, prepareImage, print, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setMixingCutoutShape, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate
Constructor Details
List
public List() throws HeadlessException
List(0, false)
. Also note that the number of visible lines in the list cannot be changed after it has been created.- Throws:
-
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true. - See Also:
List
public List(int rows) throws HeadlessException
List(rows, false)
. Also note that the number of visible rows in the list cannot be changed after it has been created.- Parameters:
-
rows
- the number of items to show. - Throws:
-
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true. - Since:
- 1.1
- See Also:
List
public List(int rows, boolean multipleMode) throws HeadlessException
multipleMode
is true
, then the user can select multiple items from the list. If it is false
, only one item at a time can be selected.- Parameters:
-
rows
- the number of items to show. -
multipleMode
- iftrue
, then multiple selections are allowed; otherwise, only one item can be selected at a time. - Throws:
-
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true. - See Also:
Method Details
addNotify
public void addNotify()
removeNotify
public void removeNotify()
- Overrides:
-
removeNotify
in classComponent
- See Also:
getItemCount
public int getItemCount()
- Returns:
- the number of items in the list
- Since:
- 1.1
- See Also:
countItems
@Deprecated public int countItems()
getItemCount()
.- Returns:
- the number of items in the list
getItem
public String getItem(int index)
- Parameters:
-
index
- the position of the item - Returns:
- an item that is associated with the specified index
- See Also:
getItems
public String[] getItems()
- Returns:
- a string array containing items of the list
- Since:
- 1.1
- See Also:
add
public void add(String item)
- Parameters:
-
item
- the item to be added - Since:
- 1.1
addItem
@Deprecated public void addItem(String item)
add(String)
.- Parameters:
-
item
- the item to be added
add
public void add(String item, int index)
- Parameters:
-
item
- the item to be added; if this parameter isnull
then the item is treated as an empty string,""
-
index
- the position at which to add the item - Since:
- 1.1
addItem
@Deprecated public void addItem(String item, int index)
add(String, int)
.- Parameters:
-
item
- the item to be added -
index
- the position at which to add the item
replaceItem
public void replaceItem(String newValue, int index)
- Parameters:
-
newValue
- a new string to replace an existing item -
index
- the position of the item to replace - Throws:
-
ArrayIndexOutOfBoundsException
- ifindex
is out of range
removeAll
public void removeAll()
- Since:
- 1.1
- See Also:
clear
@Deprecated public void clear()
removeAll()
.remove
public void remove(String item)
- Parameters:
-
item
- the item to remove from the list - Throws:
-
IllegalArgumentException
- if the item doesn't exist in the list - Since:
- 1.1
remove
public void remove(int position)
- Parameters:
-
position
- the index of the item to delete - Throws:
-
ArrayIndexOutOfBoundsException
- if theposition
is less than 0 or greater thangetItemCount()-1
- Since:
- 1.1
- See Also:
delItem
@Deprecated public void delItem(int position)
remove(String)
and remove(int)
.- Parameters:
-
position
- the index of the item to delete
getSelectedIndex
public int getSelectedIndex()
- Returns:
- the index of the selected item; if no item is selected, or if multiple items are selected,
-1
is returned. - See Also:
getSelectedIndexes
public int[] getSelectedIndexes()
- Returns:
- an array of the selected indexes on this scrolling list; if no item is selected, a zero-length array is returned.
- See Also:
getSelectedItem
public String getSelectedItem()
- Returns:
- the selected item on the list; if no item is selected, or if multiple items are selected,
null
is returned. - See Also:
getSelectedItems
public String[] getSelectedItems()
- Returns:
- an array of the selected items on this scrolling list; if no item is selected, a zero-length array is returned.
- See Also:
getSelectedObjects
public Object[] getSelectedObjects()
- Specified by:
-
getSelectedObjects
in interfaceItemSelectable
- Returns:
- an array of
Object
s representing the selected items on this scrolling list; if no item is selected, a zero-length array is returned. - See Also:
select
public void select(int index)
Note that passing out of range parameters is invalid, and will result in unspecified behavior.
Note that this method should be primarily used to initially select an item in this component. Programmatically calling this method will not trigger an ItemEvent
. The only way to trigger an ItemEvent
is by user interaction.
- Parameters:
-
index
- the position of the item to select - See Also:
deselect
public void deselect(int index)
Note that passing out of range parameters is invalid, and will result in unspecified behavior.
If the item at the specified index is not selected, then the operation is ignored.
- Parameters:
-
index
- the position of the item to deselect - See Also:
isIndexSelected
public boolean isIndexSelected(int index)
- Parameters:
-
index
- the item to be checked - Returns:
-
true
if the specified item has been selected;false
otherwise - Since:
- 1.1
- See Also:
isSelected
@Deprecated public boolean isSelected(int index)
isIndexSelected(int)
.- Parameters:
-
index
- specifies the item to be checked - Returns:
-
true
if the item is selected; otherwisefalse
getRows
public int getRows()
List
has been created, this number will never change.- Returns:
- the number of visible lines in this scrolling list
isMultipleMode
public boolean isMultipleMode()
- Returns:
-
true
if this list allows multiple selections; otherwise,false
- Since:
- 1.1
- See Also:
allowsMultipleSelections
@Deprecated public boolean allowsMultipleSelections()
isMultipleMode()
.- Returns:
-
true
if this list allows multiple selections; otherwisefalse
setMultipleMode
public void setMultipleMode(boolean b)
- Parameters:
-
b
- iftrue
then multiple selections are allowed; otherwise, only one item from the list can be selected at once - Since:
- 1.1
- See Also:
setMultipleSelections
@Deprecated public void setMultipleSelections(boolean b)
setMultipleMode(boolean)
.- Parameters:
-
b
-true
to enable multiple mode,false
otherwise
getVisibleIndex
public int getVisibleIndex()
makeVisible
.- Returns:
- the index of the item that was last made visible
- See Also:
makeVisible
public void makeVisible(int index)
- Parameters:
-
index
- the position of the item - See Also:
getPreferredSize
public Dimension getPreferredSize(int rows)
- Parameters:
-
rows
- number of rows in the list - Returns:
- the preferred dimensions for displaying this scrolling list given that the specified number of rows must be visible
- Since:
- 1.1
- See Also:
preferredSize
@Deprecated public Dimension preferredSize(int rows)
getPreferredSize(int)
.- Parameters:
-
rows
- the number of rows - Returns:
- the preferred dimensions for displaying this list
getPreferredSize
public Dimension getPreferredSize()
- Overrides:
-
getPreferredSize
in classComponent
- Returns:
- the preferred dimensions for displaying this scrolling list
- Since:
- 1.1
- See Also:
preferredSize
@Deprecated public Dimension preferredSize()
getPreferredSize()
.Component
- Overrides:
-
preferredSize
in classComponent
- Returns:
- the component's preferred size
getMinimumSize
public Dimension getMinimumSize(int rows)
- Parameters:
-
rows
- number of rows in the list - Returns:
- the minimum dimensions for displaying this scrolling list given that the specified number of rows must be visible
- Since:
- 1.1
- See Also:
minimumSize
@Deprecated public Dimension minimumSize(int rows)
getMinimumSize(int)
.- Parameters:
-
rows
- the number of rows in the list - Returns:
- the minimum dimensions for displaying this list
getMinimumSize
public Dimension getMinimumSize()
- Overrides:
-
getMinimumSize
in classComponent
- Returns:
- the minimum dimensions needed to display this scrolling list
- Since:
- 1.1
- See Also:
minimumSize
@Deprecated public Dimension minimumSize()
getMinimumSize()
.Component
- Overrides:
-
minimumSize
in classComponent
- Returns:
- the minimum size of this component
addItemListener
public void addItemListener(ItemListener l)
select
or deselect
. If listener l
is null
, no exception is thrown and no action is performed. Refer to AWT Threading Issues for details on AWT's threading model.
- Specified by:
-
addItemListener
in interfaceItemSelectable
- Parameters:
-
l
- the item listener - Since:
- 1.1
- See Also:
removeItemListener
public void removeItemListener(ItemListener l)
l
is null
, no exception is thrown and no action is performed. Refer to AWT Threading Issues for details on AWT's threading model.
- Specified by:
-
removeItemListener
in interfaceItemSelectable
- Parameters:
-
l
- the item listener - Since:
- 1.1
- See Also:
getItemListeners
public ItemListener[] getItemListeners()
- Returns:
- all of this list's
ItemListener
s or an empty array if no item listeners are currently registered - Since:
- 1.4
- See Also:
addActionListener
public void addActionListener(ActionListener l)
If listener l
is null
, no exception is thrown and no action is performed.
Refer to AWT Threading Issues for details on AWT's threading model.
- Parameters:
-
l
- the action listener - Since:
- 1.1
- See Also:
removeActionListener
public void removeActionListener(ActionListener l)
l
is null
, no exception is thrown and no action is performed. Refer to AWT Threading Issues for details on AWT's threading model.
- Parameters:
-
l
- the action listener - Since:
- 1.1
- See Also:
getActionListeners
public ActionListener[] getActionListeners()
- Returns:
- all of this list's
ActionListener
s or an empty array if no action listeners are currently registered - Since:
- 1.4
- See Also:
getListeners
public <T extends EventListener> T[] getListeners(Class<T> listenerType)
FooListener
s upon this List
. FooListener
s are registered using the addFooListener
method. You can specify the listenerType
argument with a class literal, such as FooListener.class
. For example, you can query a List l
for its item listeners with the following code:
ItemListener[] ils = (ItemListener[])(l.getListeners(ItemListener.class));If no such listeners exist, this method returns an empty array.
- Overrides:
-
getListeners
in classComponent
- Type Parameters:
-
T
- the type of the listeners - Parameters:
-
listenerType
- the type of listeners requested; this parameter should specify an interface that descends fromjava.util.EventListener
- Returns:
- an array of all objects registered as
FooListener
s on this list, or an empty array if no such listeners have been added - Throws:
-
ClassCastException
- iflistenerType
doesn't specify a class or interface that implementsjava.util.EventListener
- Since:
- 1.3
- See Also:
processEvent
protected void processEvent(AWTEvent e)
ItemEvent
, it invokes the processItemEvent
method. Else, if the event is an instance of ActionEvent
, it invokes processActionEvent
. If the event is not an item event or an action event, it invokes processEvent
on the superclass. Note that if the event parameter is null
the behavior is unspecified and may result in an exception.
- Overrides:
-
processEvent
in classComponent
- Parameters:
-
e
- the event - Since:
- 1.1
- See Also:
processItemEvent
protected void processItemEvent(ItemEvent e)
ItemListener
objects. This method is not called unless item events are enabled for this component. Item events are enabled when one of the following occurs:
- An
ItemListener
object is registered viaaddItemListener
. - Item events are enabled via
enableEvents
.
Note that if the event parameter is null
the behavior is unspecified and may result in an exception.
- Parameters:
-
e
- the item event - Since:
- 1.1
- See Also:
processActionEvent
protected void processActionEvent(ActionEvent e)
ActionListener
objects. This method is not called unless action events are enabled for this component. Action events are enabled when one of the following occurs:
- An
ActionListener
object is registered viaaddActionListener
. - Action events are enabled via
enableEvents
.
Note that if the event parameter is null
the behavior is unspecified and may result in an exception.
- Parameters:
-
e
- the action event - Since:
- 1.1
- See Also:
paramString
protected String paramString()
- Overrides:
-
paramString
in classComponent
- Returns:
- the parameter string of this scrolling list
delItems
@Deprecated public void delItems(int start, int end)
- Parameters:
-
start
- the beginning index of the range to delete -
end
- the ending index of the range to delete
getAccessibleContext
public AccessibleContext getAccessibleContext()
AccessibleContext
associated with this List
. For lists, the AccessibleContext
takes the form of an AccessibleAWTList
. A new AccessibleAWTList
instance is created, if necessary.- Specified by:
-
getAccessibleContext
in interfaceAccessible
- Overrides:
-
getAccessibleContext
in classComponent
- Returns:
- an
AccessibleAWTList
that serves as theAccessibleContext
of thisList
- Since:
- 1.3
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/List.html
add(String)
.