This class is a facade over three separate objects:
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 |
ensureIndexIsVisible
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.
i
- The list index to make visible
getCellBounds
public Rectangle getCellBounds(int index0,
int index1)
Returns the bounds of the rectangle that encloses both list cells
with index0 and index1.
index0
- the index of the first cellindex1
- the index of the second cell
- the bounds of the rectangle that encloses both list cells
with index0 and index1,
null
if one of the indices is
not valid
getDragEnabled
public boolean getDragEnabled()
Return the value of the dragEnabled
property.
getFirstVisibleIndex
public int getFirstVisibleIndex()
- The index of the first visible list cell, or
-1
if none is visible.
getFixedCellHeight
public int getFixedCellHeight()
Gets the value of the
fixedCellHeight
property. This property
may be
-1
to indicate that no cell height has been
set. This property is also set implicitly when the
prototypeCellValue
property is set.
- The current value of the property
getFixedCellWidth
public int getFixedCellWidth()
Gets the value of the
fixedCellWidth
property. This property
may be
-1
to indicate that no cell width has been
set. This property is also set implicitly when the
prototypeCellValue
property is set.
- The current value of the property
getLastVisibleIndex
public int getLastVisibleIndex()
- The index of the last visible list cell, or
-1
if none is visible.
getLayoutOrientation
public int getLayoutOrientation()
Returns the layout orientation.
- the orientation, one of
JList.VERTICAL
,
JList.VERTICAL_WRAP
and JList.HORIZONTAL_WRAP
getNextMatch
public int getNextMatch(String prefix,
int startIndex,
Position.Bias bias)
Returns the next list element (beginning from startIndex
that starts with prefix
. Searching is done in the direction
specified by bias
.
- the index of the found element or -1 if no such element has
been found
getScrollableBlockIncrement
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 is
HORIZONTAL
, the above
comments refer to "rightwards" for positive direction, and "leftwards"
for negative.
- getScrollableBlockIncrement in interface Scrollable
visibleRect
- The rectangle to scroll an element intoorientation
- One of the numeric consants VERTICAL
or HORIZONTAL
direction
- An integer indicating the scroll direction: positive means
forwards (down, right), negative means backwards (up, left)
- The scrollable unit increment, in pixels
getScrollableTracksViewportHeight
public boolean getScrollableTracksViewportHeight()
Gets the value of the scrollableTracksViewportWidth property.
- getScrollableTracksViewportHeight in interface Scrollable
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
getScrollableTracksViewportWidth
public boolean getScrollableTracksViewportWidth()
Gets the value of the scrollableTracksViewportWidth
property.
- getScrollableTracksViewportWidth in interface Scrollable
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
getScrollableUnitIncrement
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 is
HORIZONTAL
, the above
comments refer to "rightwards" for positive direction, and "leftwards"
for negative.
- getScrollableUnitIncrement in interface Scrollable
visibleRect
- The rectangle to scroll an element intoorientation
- One of the numeric consants VERTICAL
or HORIZONTAL
direction
- An integer indicating the scroll direction: positive means
forwards (down, right), negative means backwards (up, left)
- The scrollable unit increment, in pixels
getSelectedIndex
public int getSelectedIndex()
Returns the minimum index of an element in the list which is currently
selected.
- A number in the half-open range
[0, x)
where
x = 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
getSelectedIndices
public int[] getSelectedIndices()
Returns the indices of values in the
model
property which are
selected.
- An array of model indices, each of which is selected according
to the
getSelectedValues()
property
getSelectedValue
public Object getSelectedValue()
Returns the first value in the list's
model
property which is
selected, according to the list's
selectionModel
property.
This is equivalent to calling
getModel()getElementAt(getSelectedIndex())
, with a check
for the special index value of
-1
which returns null
null
.
- The first selected element, or
null
if no element
is selected.
getSelectedValues
public Object[] getSelectedValues()
Returns all the values in the list's
model
property which
are selected, according to the list's
selectionModel
property.
- An array containing all the selected values
getUI
public ListUI getUI()
Gets the value of the UI property.
- The current property value
getValueIsAdjusting
public boolean getValueIsAdjusting()
Returns the value of the valueIsAdjusting
property.
indexToLocation
public Point indexToLocation(int index)
Returns location of the cell located at the specified index in the list.
index
- of the cell for which location will be determined
- location of the cell located at the specified index in the list.
isSelectedIndex
public boolean isSelectedIndex(int a)
Indicates whether the list element at a given index value is
currently selected.
true
if a
is the index of a selected
list element
isSelectionEmpty
public boolean isSelectionEmpty()
Returns true
if the model's selection is empty, otherwise
false
.
locationToIndex
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.
location
- for which to look for in the list
- index of the cell to which specified location is closest to.
setDragEnabled
public void setDragEnabled(boolean enabled)
Set the dragEnabled
property.
setLayoutOrientation
public void setLayoutOrientation(int orientation)
Sets the layout orientation.
orientation
- the orientation to set, one of JList.VERTICAL
,
JList.VERTICAL_WRAP
and JList.HORIZONTAL_WRAP
setListData
public void setListData(Object[] listData)
Sets the
model
property of the list to a new anonymous
AbstractListModel
subclass which accesses the provided Object
array directly.
listData
- The object array to build a new list model on
setListData
public void setListData(Vector listData)
Sets the
model
property of the list to a new anonymous
AbstractListModel
subclass which accesses the provided vector
directly.
listData
- The object array to build a new list model on
setModel
public void setModel(ListModel model)
Sets the value of the
model
property. The list's
listListener
is unsubscribed from the existing model, if it exists,
and re-subscribed to the new model.
model
- the new model (null
not permitted).
setPrototypeCellValue
public void setPrototypeCellValue(Object obj)
Set the
prototypeCellValue
property. This property holds a
reference to a "prototype" data value -- typically a String -- which
is used to calculate the
fixedCellWidth
and
fixedCellHeight
properties, using the
cellRenderer
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.
obj
- The new prototype cell value
setSelectedIndex
public void setSelectedIndex(int a)
Adds the interval
[a,a]
to the set of selections managed
by this list's
selectionModel
property. Depending on the
selection mode, this may cause existing selections to become invalid,
or may simply expand the set of selections.
a
- A number in the half-open range [0, x)
where
x = getModel.getSize()
, indicating the index of an
element in the list to select. When < 0 the selection is cleared.
setSelectedValue
public void setSelectedValue(Object obj,
boolean scroll)
Sets the selection to cover only the specified value, if it
exists in the model.
obj
- The object to selectscroll
- Whether to scroll the list to make the newly selected
value visible
setSelectionMode
public void setSelectionMode(int a)
Sets the list's "selectionMode" property, which simply mirrors the
same property on the list's
selectionModel
property. This
property should be one of the integer constants
SINGLE_SELECTION
,
SINGLE_INTERVAL_SELECTION
,
or
MULTIPLE_INTERVAL_SELECTION
from the
ListSelectionModel
interface.
a
- The new selection mode
setUI
public void setUI(ListUI ui)
Sets the value of the UI property.
ui
- The new property value
setValueIsAdjusting
public void setValueIsAdjusting(boolean isAdjusting)
Sets the valueIsAdjusting
property.
isAdjusting
- the new value
JList.java --
Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.