Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
ListModel
, ListSelectionModel
and
ListUI
. The facade represents a unified "list"
concept, with independently replacable (possibly client-provided) models
for its contents and its current selection. In addition, each element in
the list is rendered via a strategy class ListCellRenderer
.
Lists have many properties, some of which are stored in this class
while others are delegated to the list's model or selection. The
following properties are available:
Property | Stored in | Bound? |
---|---|---|
accessibleContext | list | no |
anchorSelectionIndex | selection | no |
cellRenderer | list | yes |
dragEnabled | list | no |
firstVisibleIndex | list | no |
fixedCellHeight | list | yes |
fixedCellWidth | list | yes |
lastVisibleIndex | list | no |
layoutOrientation | list | yes |
leadSelectionIndex | selection | no |
maxSelectionIndex | selection | no |
minSelectionIndex | selection | no |
model | list | yes |
opaque | list | no |
preferredScrollableViewportSize | list | no |
prototypeCellValue | list | yes |
scrollableTracksViewportHeight | list | no |
scrollableTracksViewportWidth | list | no |
selectedIndex | selection | no |
selectedIndices | selection | no |
selectedValue | model | no |
selectedValues | model | no |
selectionBackground | list | yes |
selectionEmpty | selection | no |
selectionForeground | list | yes |
selectionMode | selection | no |
selectionModel | list | yes |
UI | list | yes |
UIClassID | list | no |
valueIsAdjusting | list | no |
visibleRowCount | list | no |
Nested Class Summary | |
protected class |
|
Nested classes/interfaces inherited from class javax.swing.JComponent | |
JComponent.AccessibleJComponent |
Nested classes/interfaces inherited from class java.awt.Container | |
Container.AccessibleAWTContainer |
Nested classes/interfaces inherited from class java.awt.Component | |
Component.AccessibleAWTComponent , Component.BltBufferStrategy , Component.FlipBufferStrategy |
Field Summary | |
static int |
|
static int |
|
static int |
|
Fields inherited from class javax.swing.JComponent | |
TOOL_TIP_TEXT_KEY , UNDEFINED_CONDITION , WHEN_ANCESTOR_OF_FOCUSED_COMPONENT , WHEN_FOCUSED , WHEN_IN_FOCUSED_WINDOW , accessibleContext , listenerList , ui |
Fields inherited from class java.awt.Component | |
BOTTOM_ALIGNMENT , CENTER_ALIGNMENT , LEFT_ALIGNMENT , RIGHT_ALIGNMENT , TOP_ALIGNMENT |
Fields inherited from interface java.awt.image.ImageObserver | |
ABORT , ALLBITS , ERROR , FRAMEBITS , HEIGHT , PROPERTIES , SOMEBITS , WIDTH |
Constructor Summary | |
| |
| |
Method Summary | |
void |
|
void |
|
void |
|
protected ListSelectionModel |
|
void |
|
protected void |
|
AccessibleContext |
|
int |
|
Rectangle |
|
ListCellRenderer |
|
boolean |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
ListSelectionListener[] |
|
int |
|
int |
|
ListModel | |
int |
|
Dimension |
|
Object |
|
int |
|
boolean |
|
boolean |
|
int |
|
int |
|
int[] |
|
Object |
|
Object[] |
|
Color |
|
Color |
|
int |
|
ListSelectionModel |
|
ListUI |
|
String |
|
boolean |
|
int |
|
Point |
|
boolean |
|
boolean |
|
int |
|
protected String |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void | |
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void | |
void |
|
void |
|
void |
|
Methods inherited from class java.lang.Object | |
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
public static final int HORIZONTAL_WRAP
Constant value used in "layoutOrientation" property. This value means that cells are laid out in multiple columns "newspaper style", filling horizontally first, then vertically.
- Field Value:
- 2
public static final int VERTICAL
Constant value used in "layoutOrientation" property. This value means that cells are laid out in a single vertical column. This is the default.
- Field Value:
- 0
public static final int VERTICAL_WRAP
Constant value used in "layoutOrientation" property. This value means that cells are laid out in multiple columns "newspaper style", filling vertically first, then horizontally.
- Field Value:
- 1
public JList(Vector> items)
Creates a newJList
object.
- Parameters:
items
- the initial list items.
public JList(Object[] items)
Creates a newJList
object.
- Parameters:
items
- the initial list items.
public JList(ListModel model)
Creates a newJList
object.
- Parameters:
model
- a model containing the list items (null
not permitted).
- Throws:
IllegalArgumentException
- ifmodel
isnull
.
public void addListSelectionListener(ListSelectionListener listener)
Adds aListSelectionListener
to the listener list for this list. The listener will be called back with aListSelectionEvent
any time the list'sselectionModel
property changes. The source of such events will be the JList, not the selection model.
- Parameters:
listener
- The new listener to add
public void addSelectionInterval(int anchor, int lead)
Adds the specified interval to the current selection. Note thatanchor
can be less than, equal to, or greater thanlead
.
- Parameters:
anchor
- the index of the anchor item.lead
- the index of the lead item.
protected ListSelectionModel createSelectionModel()
Creates the defaultListSelectionModel
.
- Returns:
- the
ListSelectionModel
public void ensureIndexIsVisible(int i)
Scrolls this list to make the specified cell visible. This only works if the list is contained within a viewport.
- Parameters:
i
- The list index to make visible
protected void fireSelectionValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)
Fire aListSelectionEvent
to all the registered ListSelectionListeners.
- Parameters:
firstIndex
- the lowest index covering the selection change.lastIndex
- the highest index covering the selection change.isAdjusting
- a flag indicating if this event is one in a series of events updating the selection.
public AccessibleContext getAccessibleContext()
Returns the accessibility framework context of this class. Component is not accessible, so the default implementation returns null. Subclasses must override this behavior, and return an appropriate subclass ofComponent.AccessibleAWTComponent
.
- Specified by:
- getAccessibleContext in interface Accessible
- Overrides:
- getAccessibleContext in interface JComponent
- Returns:
- the accessibility context
public int getAnchorSelectionIndex()
Returns the index of the anchor item in the current selection, or-1
if there is no anchor item.
- Returns:
- The item index.
public Rectangle getCellBounds(int index0, int index1)
Returns the bounds of the rectangle that encloses both list cells with index0 and index1.
- Parameters:
index0
- the index of the first cellindex1
- the index of the second cell
- Returns:
- the bounds of the rectangle that encloses both list cells with index0 and index1,
null
if one of the indices is not valid
public ListCellRenderer getCellRenderer()
Gets the value of thecellRenderer
property.
- Returns:
- The current value of the property
public boolean getDragEnabled()
Return the value of thedragEnabled
property.
- Returns:
- the value
- Since:
- 1.4
public int getFirstVisibleIndex()
Returns the list index of the upper left or upper right corner of the visible rectangle of this list, depending on theComponent.getComponentOrientation()
property.
- Returns:
- The index of the first visible list cell, or
-1
if none is visible.
public int getFixedCellHeight()
Gets the value of thefixedCellHeight
property. This property may be-1
to indicate that no cell height has been set. This property is also set implicitly when theprototypeCellValue
property is set.
- Returns:
- The current value of the property
public int getFixedCellWidth()
Gets the value of thefixedCellWidth
property. This property may be-1
to indicate that no cell width has been set. This property is also set implicitly when theprototypeCellValue
property is set.
- Returns:
- The current value of the property
public int getLastVisibleIndex()
Returns the list index of the lower right or lower left corner of the visible rectangle of this list, depending on theComponent.getComponentOrientation()
property.
- Returns:
- The index of the last visible list cell, or
-1
if none is visible.
public int getLayoutOrientation()
Returns the layout orientation, which will be one ofVERTICAL
,VERTICAL_WRAP
andHORIZONTAL_WRAP
. The default value isVERTICAL
.
- Returns:
- the orientation.
- Since:
- 1.4
- See Also:
setLayoutOrientation(int)
public int getLeadSelectionIndex()
Returns the index of the lead item in the current selection, or-1
if there is no lead item.
- Returns:
- The item index.
public ListSelectionListener[] getListSelectionListeners()
Returns an array of all ListSelectionListeners subscribed to this list.
- Returns:
- The current subscribed listeners
- Since:
- 1.4
public int getMaxSelectionIndex()
Returns the highest item index in the current selection, or-1
if there is no selection.
- Returns:
- The index.
- See Also:
getMinSelectionIndex()
public int getMinSelectionIndex()
Returns the lowest item index in the current selection, or-1
if there is no selection.
- Returns:
- The index.
- See Also:
getMaxSelectionIndex()
public ListModel getModel()
Gets the value of themodel
property.
- Returns:
- The current value of the property
public int getNextMatch(String prefix, int startIndex, Position.Bias direction)
Returns the index of the next list element (beginning atstartIndex
and moving in the specified direction through the list, looping around if necessary) that starts withprefix
(ignoring case).
- Parameters:
prefix
- the prefix to search for in the cell valuesstartIndex
- the index where to start searching fromdirection
- the search direction, eitherPosition.Bias.Forward
orPosition.Bias.Backward
(null
is interpreted asPosition.Bias.Backward
.
- Returns:
- the index of the found element or -1 if no such element has been found
- Throws:
IllegalArgumentException
- if prefix isnull
or startIndex is not valid
- Since:
- 1.4
public Dimension getPreferredScrollableViewportSize()
Returns a size indicating how much space this list would like to consume, when contained in a scrollable viewport. This is part of theScrollable
interface, which interacts withScrollPaneLayout
andJViewport
to define scrollable objects.
- Specified by:
- getPreferredScrollableViewportSize in interface Scrollable
- Returns:
- The preferred size
public Object getPrototypeCellValue()
Returns the current value of theprototypeCellValue
property. This property holds a reference to a "prototype" data value -- typically a String -- which is used to calculate thefixedCellWidth
andfixedCellHeight
properties, using thecellRenderer
property to acquire a component to render the prototype.
- Returns:
- The current prototype cell value
- See Also:
setPrototypeCellValue(Object)
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
Return the number of pixels the list must scroll in order to move a "block" of the list into the provided visible rectangle. When the provided direction is positive, the call describes a "downwards" scroll, which will be exposing a cell at a greater index in the list than those elements currently showing. Then the provided direction is negative, the call describes an "upwards" scroll, which will be exposing a cell at a lesser index in the list than those elements currently showing. If the provided orientation isHORIZONTAL
, the above comments refer to "rightwards" for positive direction, and "leftwards" for negative.
- Specified by:
- getScrollableBlockIncrement in interface Scrollable
- Parameters:
visibleRect
- The rectangle to scroll an element intoorientation
- One of the numeric consantsVERTICAL
orHORIZONTAL
direction
- An integer indicating the scroll direction: positive means forwards (down, right), negative means backwards (up, left)
- Returns:
- The scrollable unit increment, in pixels
public boolean getScrollableTracksViewportHeight()
Gets the value of the scrollableTracksViewportWidth property.
- Specified by:
- getScrollableTracksViewportHeight in interface Scrollable
- Returns:
true
if the viewport is larger (vertically) than the list and the list should be expanded to fit the viewport;false
if the viewport is smaller than the list and the list should scroll (vertically) within the viewport
public boolean getScrollableTracksViewportWidth()
Gets the value of thescrollableTracksViewportWidth
property.
- Specified by:
- getScrollableTracksViewportWidth in interface Scrollable
- Returns:
true
if the viewport is larger (horizontally) than the list and the list should be expanded to fit the viewport;false
if the viewport is smaller than the list and the list should scroll (horizontally) within the viewport
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
Return the number of pixels the list must scroll in order to move a "unit" of the list into the provided visible rectangle. When the provided direction is positive, the call describes a "downwards" scroll, which will be exposing a cell at a greater index in the list than those elements currently showing. Then the provided direction is negative, the call describes an "upwards" scroll, which will be exposing a cell at a lesser index in the list than those elements currently showing. If the provided orientation isHORIZONTAL
, the above comments refer to "rightwards" for positive direction, and "leftwards" for negative.
- Specified by:
- getScrollableUnitIncrement in interface Scrollable
- Parameters:
visibleRect
- The rectangle to scroll an element intoorientation
- One of the numeric consantsVERTICAL
orHORIZONTAL
direction
- An integer indicating the scroll direction: positive means forwards (down, right), negative means backwards (up, left)
- Returns:
- The scrollable unit increment, in pixels
public int getSelectedIndex()
Returns the minimum index of an element in the list which is currently selected.
- Returns:
- A number in the half-open range
[0, x)
wherex = getModel.getSize()
, indicating the minimum index of an element in the list for which the element is selected, or-1
if no elements are selected
public int[] getSelectedIndices()
Returns the indices of values in themodel
property which are selected.
- Returns:
- An array of model indices, each of which is selected according to the
getSelectedValues()
property
public Object getSelectedValue()
Returns the first value in the list'smodel
property which is selected, according to the list'sselectionModel
property. This is equivalent to callinggetModel()getElementAt(getSelectedIndex())
, with a check for the special index value of-1
which returns nullnull
.
- Returns:
- The first selected element, or
null
if no element is selected.
- See Also:
getSelectedValues()
public Object[] getSelectedValues()
Returns all the values in the list'smodel
property which are selected, according to the list'sselectionModel
property.
- Returns:
- An array containing all the selected values
- See Also:
setSelectedValue(Object,boolean)
public Color getSelectionBackground()
Gets the value of theselectionBackground
property.
- Returns:
- The current value of the property
public Color getSelectionForeground()
Gets the value of theselectionForeground
property.
- Returns:
- The current value of the property
public int getSelectionMode()
Returns the selection mode for the list (one of:ListSelectionModel.SINGLE_SELECTION
,ListSelectionModel.SINGLE_INTERVAL_SELECTION
andListSelectionModel.MULTIPLE_INTERVAL_SELECTION
).
- Returns:
- The selection mode.
- See Also:
setSelectionMode(int)
public ListSelectionModel getSelectionModel()
Returns the selection model for theJList
component. Note that this class contains a range of convenience methods for configuring the selection model:
- Returns:
- The selection model.
public String getUIClassID()
Return the class identifier for the list's UI property. This should be the constant string"ListUI"
, and map to an appropriate UI class in theUIManager
.
- Overrides:
- getUIClassID in interface JComponent
- Returns:
- The class identifier
public boolean getValueIsAdjusting()
Returns thevalueIsAdjusting
flag from the list's selection model.
- Returns:
- the value
public int getVisibleRowCount()
Gets the value of thevisibleRowCount
property. The default value is 8.
- Returns:
- the current value of the property.
- See Also:
setVisibleRowCount(int)
public Point indexToLocation(int index)
Returns location of the cell located at the specified index in the list.
- Parameters:
index
- of the cell for which location will be determined
- Returns:
- location of the cell located at the specified index in the list.
public boolean isSelectedIndex(int a)
Indicates whether the list element at a given index value is currently selected.
- Parameters:
a
- The index to check
- Returns:
true
ifa
is the index of a selected list element
public boolean isSelectionEmpty()
Returnstrue
if the model's selection is empty, otherwisefalse
.
- Returns:
- The return value of
ListSelectionModel.isSelectionEmpty()
public int locationToIndex(Point location)
Returns index of the cell to which specified location is closest to. If the location is outside the bounds of the list, then the greatest index in the list model is returned. If the list model is empty, then-1
is returned.
- Parameters:
location
- for which to look for in the list
- Returns:
- index of the cell to which specified location is closest to.
protected String paramString()
Returns a string describing the attributes for theJList
component, for use in debugging. The return value is guaranteed to be non-null
, but the format of the string may vary between implementations.
- Overrides:
- paramString in interface JComponent
- Returns:
- A string describing the attributes of the
JList
.
public void removeListSelectionListener(ListSelectionListener listener)
Removes aListSelectionListener
from the listener list for this list. The listener will no longer be called when the list'sselectionModel
changes.
- Parameters:
listener
- The listener to remove
public void removeSelectionInterval(int index0, int index1)
Removes the specified interval from the current selection. Note thatindex0
can be less than, equal to, or greater thanindex1
.
- Parameters:
index0
- an index for one end of the range.index1
- an index for the other end of the range.
public void setCellRenderer(ListCellRenderer renderer)
Sets the value of thegetCellRenderer()
property.
- Parameters:
renderer
- The new property value
public void setDragEnabled(boolean enabled)
Set thedragEnabled
property.
- Parameters:
enabled
- new value
- Since:
- 1.4
public void setFixedCellHeight(int h)
Sets the value of thefixedCellHeight
property. This property may be-1
to indicate that no cell height has been set. This property is also set implicitly when theprototypeCellValue
property is set, but setting it explicitly overrides the height computed fromprototypeCellValue
.
- Parameters:
h
- the height.
- See Also:
getFixedCellHeight()
,getPrototypeCellValue()
public void setFixedCellWidth(int w)
Sets the value of thefixedCellWidth
property. This property may be-1
to indicate that no cell width has been set. This property is also set implicitly when theprototypeCellValue
property is set, but setting it explicitly overrides the width computed fromprototypeCellValue
.
- Parameters:
w
- the width.
- See Also:
getFixedCellHeight()
,getPrototypeCellValue()
public void setLayoutOrientation(int orientation)
Sets the layout orientation (this is a bound property with the name 'layoutOrientation'). Valid orientations areVERTICAL
,VERTICAL_WRAP
andHORIZONTAL_WRAP
.
- Parameters:
orientation
- the orientation.
- Throws:
IllegalArgumentException
- iforientation
is not one of the specified values.
- Since:
- 1.4
- See Also:
getLayoutOrientation()
public void setListData(Vector> listData)
Sets themodel
property of the list to a new anonymousAbstractListModel
subclass which accesses the provided vector directly.
- Parameters:
listData
- The object array to build a new list model on
- See Also:
setModel(ListModel)
public void setListData(Object[] listData)
Sets themodel
property of the list to a new anonymousAbstractListModel
subclass which accesses the provided Object array directly.
- Parameters:
listData
- The object array to build a new list model on
- See Also:
setModel(ListModel)
public void setModel(ListModel model)
Sets the value of themodel
property. The list'slistListener
is unsubscribed from the existing model, if it exists, and re-subscribed to the new model.
- Parameters:
model
- the new model (null
not permitted).
- Throws:
IllegalArgumentException
- ifmodel
isnull
.
public void setPrototypeCellValue(Object obj)
Set theprototypeCellValue
property. This property holds a reference to a "prototype" data value -- typically a String -- which is used to calculate thefixedCellWidth
andfixedCellHeight
properties, using thecellRenderer
property to acquire a component to render the prototype. It is important that you not set this value to a component. It has to be a data value such as the objects you would find in the list's model. Setting it to a component will have undefined (and undesirable) affects.
- Parameters:
obj
- The new prototype cell value
- See Also:
getPrototypeCellValue()
public void setSelectedIndex(int a)
Adds the interval[a,a]
to the set of selections managed by this list'sselectionModel
property. Depending on the selection mode, this may cause existing selections to become invalid, or may simply expand the set of selections.
- Parameters:
a
- A number in the half-open range[0, x)
wherex = getModel.getSize()
, indicating the index of an element in the list to select. When < 0 the selection is cleared.
- See Also:
setSelectionMode(int)
,selectionModel
public void setSelectedIndices(int[] a)
- Parameters:
a
- an array of selected indices (null
not permitted).
- Throws:
NullPointerException
- ifa
isnull
.
- See Also:
setSelectionMode(int)
,selectionModel
public void setSelectedValue(Object obj, boolean scroll)
Sets the selection to cover only the specified value, if it exists in the model.
- Parameters:
obj
- The object to selectscroll
- Whether to scroll the list to make the newly selected value visible
- See Also:
ensureIndexIsVisible(int)
public void setSelectionBackground(Color c)
Sets the value of theselectionBackground
property.
- Parameters:
c
- The new value of the property
public void setSelectionForeground(Color c)
Sets the value of theselectionForeground
property.
- Parameters:
c
- The new value of the property
public void setSelectionInterval(int anchor, int lead)
Sets the current selection to the items in the specified range (inclusive). Note thatanchor
can be less than, equal to, or greater thanlead
.
- Parameters:
anchor
- the index of the anchor item.lead
- the index of the anchor item.
public void setSelectionMode(int a)
Sets the list's "selectionMode" property, which simply mirrors the same property on the list'sselectionModel
property. This property should be one of the integer constantsSINGLE_SELECTION
,SINGLE_INTERVAL_SELECTION
, orMULTIPLE_INTERVAL_SELECTION
from theListSelectionModel
interface.
- Parameters:
a
- The new selection mode
public void setSelectionModel(ListSelectionModel model)
Sets the value of theselectionModel
property. The list'slistListener
is unsubscribed from the existing selection model, if it exists, and re-subscribed to the new selection model.
- Parameters:
model
- The new property value
public void setUI(ListUI ui)
Sets the value of the UI property.
- Parameters:
ui
- The new property value
public void setValueIsAdjusting(boolean isAdjusting)
Sets thevalueIsAdjusting
flag in the list's selection model.
- Parameters:
isAdjusting
- the new value
public void setVisibleRowCount(int vc)
Sets the value of thevisibleRowCount
property.
- Parameters:
vc
- The new property value
- See Also:
getVisibleRowCount()