java.lang.Object java.awt.Component java.awt.List
public class List
The 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.
ItemEvent
, ItemListener
, ActionEvent
, ActionListener
, Serialized Form
protected class
List.AccessibleAWTList
List
class. Constructor Summary List()
List(int rows)
List(int rows, boolean multipleMode)
void
add(String item)
void
add(String item, int index)
void
addActionListener(ActionListener l)
void
addItem(String item)
add(String)
. void
addItem(String item, int index)
add(String, int)
. void
addItemListener(ItemListener l)
void
addNotify()
boolean
allowsMultipleSelections()
isMultipleMode()
. void
clear()
removeAll()
. int
countItems()
getItemCount()
. void
delItem(int position)
remove(String)
and remove(int)
. void
delItems(int start, int end)
void
deselect(int index)
AccessibleContext
getAccessibleContext()
AccessibleContext
associated with this List
. ActionListener[]
getActionListeners()
String
getItem(int index)
int
getItemCount()
ItemListener[]
getItemListeners()
String[]
getItems()
<T extends EventListener>
T[]
getListeners(Class<T> listenerType)
FooListener
s upon this List
. Dimension
getMinimumSize()
Dimension
getMinimumSize(int rows)
Dimension
getPreferredSize()
Dimension
getPreferredSize(int rows)
int
getRows()
int
getSelectedIndex()
int[]
getSelectedIndexes()
String
getSelectedItem()
String[]
getSelectedItems()
Object[]
getSelectedObjects()
int
getVisibleIndex()
makeVisible
. boolean
isIndexSelected(int index)
boolean
isMultipleMode()
boolean
isSelected(int index)
isIndexSelected(int)
. void
makeVisible(int index)
Dimension
minimumSize()
getMinimumSize()
. Dimension
minimumSize(int rows)
getMinimumSize(int)
. protected String
paramString()
Dimension
preferredSize()
getPreferredSize()
. Dimension
preferredSize(int rows)
getPreferredSize(int)
. protected void
processActionEvent(ActionEvent e)
ActionListener
objects. protected void
processEvent(AWTEvent e)
protected void
processItemEvent(ItemEvent e)
ItemListener
objects. void
remove(int position)
void
remove(String item)
void
removeActionListener(ActionListener l)
void
removeAll()
void
removeItemListener(ItemListener l)
void
removeNotify()
void
replaceItem(String newValue, int index)
void
select(int index)
void
setMultipleMode(boolean b)
void
setMultipleSelections(boolean b)
setMultipleMode(boolean)
. Methods inherited from 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, 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, getPeer, 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, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate
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.
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true.
GraphicsEnvironment.isHeadless()
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.
rows
- the number of items to show.
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true.
GraphicsEnvironment.isHeadless()
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.
rows
- the number of items to show.
multipleMode
- if true
, then multiple selections are allowed; otherwise, only one item can be selected at a time.
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true.
GraphicsEnvironment.isHeadless()
public void addNotify()
addNotify
in class Component
Component.isDisplayable()
, Component.removeNotify()
public void removeNotify()
removeNotify
in class Component
Component.isDisplayable()
, Component.addNotify()
public int getItemCount()
getItem(int)
@Deprecated public int countItems()
getItemCount()
.
public String getItem(int index)
index
- the position of the item
getItemCount()
public String[] getItems()
select(int)
, deselect(int)
, isIndexSelected(int)
public void add(String item)
item
- the item to be added
@Deprecated public void addItem(String item)
add(String)
.
public void add(String item, int index)
item
- the item to be added; if this parameter is null
then the item is treated as an empty string, ""
index
- the position at which to add the item
@Deprecated public void addItem(String item, int index)
add(String, int)
.
public void replaceItem(String newValue, int index)
newValue
- a new string to replace an existing item
index
- the position of the item to replace
ArrayIndexOutOfBoundsException
- if index
is out of range
public void removeAll()
remove(java.lang.String)
, delItems(int, int)
@Deprecated public void clear()
removeAll()
.
public void remove(String item)
item
- the item to remove from the list
IllegalArgumentException
- if the item doesn't exist in the list
public void remove(int position)
position
- the index of the item to delete
ArrayIndexOutOfBoundsException
- if the position
is less than 0 or greater than getItemCount()-1
add(String, int)
@Deprecated public void delItem(int position)
remove(String)
and remove(int)
.
public int getSelectedIndex()
-1
is returned.
select(int)
, deselect(int)
, isIndexSelected(int)
public int[] getSelectedIndexes()
select(int)
, deselect(int)
, isIndexSelected(int)
public String getSelectedItem()
null
is returned.
select(int)
, deselect(int)
, isIndexSelected(int)
public String[] getSelectedItems()
select(int)
, deselect(int)
, isIndexSelected(int)
public Object[] getSelectedObjects()
getSelectedObjects
in interface ItemSelectable
Object
s representing the selected items on this scrolling list; if no item is selected, a zero-length array is returned.
getSelectedItems()
, ItemSelectable
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.
index
- the position of the item to select
getSelectedItem()
, deselect(int)
, isIndexSelected(int)
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.
index
- the position of the item to deselect
select(int)
, getSelectedItem()
, isIndexSelected(int)
public boolean isIndexSelected(int index)
index
- the item to be checked
true
if the specified item has been selected; false
otherwise
select(int)
, deselect(int)
@Deprecated public boolean isSelected(int index)
isIndexSelected(int)
.
public int getRows()
List
has been created, this number will never change.
public boolean isMultipleMode()
true
if this list allows multiple selections; otherwise, false
setMultipleMode(boolean)
@Deprecated public boolean allowsMultipleSelections()
isMultipleMode()
.
public void setMultipleMode(boolean b)
b
- if true
then multiple selections are allowed; otherwise, only one item from the list can be selected at once
isMultipleMode()
@Deprecated public void setMultipleSelections(boolean b)
setMultipleMode(boolean)
.
public int getVisibleIndex()
makeVisible
.
makeVisible(int)
public void makeVisible(int index)
index
- the position of the item
getVisibleIndex()
public Dimension getPreferredSize(int rows)
rows
- number of rows in the list
Component.getPreferredSize()
@Deprecated public Dimension preferredSize(int rows)
getPreferredSize(int)
.
public Dimension getPreferredSize()
getPreferredSize
in class Component
Component.getPreferredSize()
@Deprecated public Dimension preferredSize()
getPreferredSize()
.
preferredSize
in class Component
public Dimension getMinimumSize(int rows)
rows
- number of rows in the list
Component.getMinimumSize()
@Deprecated public Dimension minimumSize(int rows)
getMinimumSize(int)
.
public Dimension getMinimumSize()
getMinimumSize
in class Component
Component.getMinimumSize()
@Deprecated public Dimension minimumSize()
getMinimumSize()
.
minimumSize
in class Component
public void addItemListener(ItemListener l)
select
or deselect
. If listener l
is null
, no exception is thrown and no action is performed.
addItemListener
in interface ItemSelectable
l
- the item listener
removeItemListener(java.awt.event.ItemListener)
, getItemListeners()
, select(int)
, deselect(int)
, ItemEvent
, ItemListener
public void removeItemListener(ItemListener l)
l
is null
, no exception is thrown and no action is performed.
removeItemListener
in interface ItemSelectable
l
- the item listener
addItemListener(java.awt.event.ItemListener)
, getItemListeners()
, ItemEvent
, ItemListener
public ItemListener[] getItemListeners()
ItemListener
s or an empty array if no item listeners are currently registered
addItemListener(java.awt.event.ItemListener)
, removeItemListener(java.awt.event.ItemListener)
, ItemEvent
, ItemListener
public void addActionListener(ActionListener l)
If listener l
is null
, no exception is thrown and no action is performed.
l
- the action listener
removeActionListener(java.awt.event.ActionListener)
, getActionListeners()
, ActionEvent
, ActionListener
public void removeActionListener(ActionListener l)
l
is null
, no exception is thrown and no action is performed.
l
- the action listener
addActionListener(java.awt.event.ActionListener)
, getActionListeners()
, ActionEvent
, ActionListener
public ActionListener[] getActionListeners()
ActionListener
s or an empty array if no action listeners are currently registered
addActionListener(java.awt.event.ActionListener)
, removeActionListener(java.awt.event.ActionListener)
, ActionEvent
, ActionListener
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.
getListeners
in class Component
listenerType
- the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
FooListener
s on this list, or an empty array if no such listeners have been added
ClassCastException
- if listenerType
doesn't specify a class or interface that implements java.util.EventListener
getItemListeners()
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.
processEvent
in class Component
e
- the event
ActionEvent
, ItemEvent
, processActionEvent(java.awt.event.ActionEvent)
, processItemEvent(java.awt.event.ItemEvent)
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:
ItemListener
object is registered via addItemListener
.enableEvents
.Note that if the event parameter is null
the behavior is unspecified and may result in an exception.
e
- the item event
ItemEvent
, ItemListener
, addItemListener(java.awt.event.ItemListener)
, Component.enableEvents(long)
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:
ActionListener
object is registered via addActionListener
.enableEvents
.Note that if the event parameter is null
the behavior is unspecified and may result in an exception.
e
- the action event
ActionEvent
, ActionListener
, addActionListener(java.awt.event.ActionListener)
, Component.enableEvents(long)
protected String paramString()
paramString
in class Component
@Deprecated public void delItems(int start, int end)
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.
getAccessibleContext
in interface Accessible
getAccessibleContext
in class Component
AccessibleAWTList
that serves as the AccessibleContext
of this List
Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4