A component that allows the user to select one or more objects from a
list. A separate model, ListModel
, represents the contents
of the list. It's easy to display an array objects, using
a JList
constructor that builds a ListModel
instance for you:
// Create a JList that displays the strings in data[]
var data:Array = ["one", "two", "three", "four"];
var dataList:JList = new JList(data);
// The value of the JList model property is an object that provides
// a read-only view of the data. It was constructed automatically.
for(int i = 0; i < dataList.getModel().getSize(); i++) {
System.out.println(dataList.getModel().getElementAt(i));
}
// Create a JList that displays the values in a IVector--VectorListModel
.
var vec:VectorListModel = new VectorListModel(["one", "two", "three", "four"]);
var vecList:JList = new JList(vec);
//When you add elements to the vector, the JList will be automatically updated.
vec.append("five");
JList
doesn't support scrolling directly.
To create a scrolling
list you make the JList
the viewport of a
JScrollPane
. For example:
JScrollPane scrollPane = new JScrollPane(dataList); // Or in two steps: JScrollPane scrollPane = new JScrollPane(); scrollPane.setView(dataList);
By default the JList
selection model is
SINGLE_SELECTION
.
String[] data = {"one", "two", "three", "four"}; JList dataList = new JList(data); dataList.setSelectedIndex(1); // select "two" dataList.getSelectedValue(); // returns "two"
The contents of a JList
can be dynamic,
in other words, the list elements can
change value and the size of the list can change after the
JList
has
been created. The JList
observes changes in its model with a
ListDataListener
implementation. A correct
implementation of ListModel
notifies
it's listeners each time a change occurs. The changes are
characterized by a ListDataEvent
, which identifies
the range of list indices that have been modified, added, or removed.
Simple dynamic-content JList
applications can use the
VectorListModel
class to store list elements. This class
implements the ListModel
and IVector
interfaces
and provides the Vector API. Applications that need to
provide custom ListModel
implementations can subclass
AbstractListModel
, which provides basic
ListDataListener
support.
JList
uses a Component
provision, provided by
a delegate called the
ListCell
, to paint the visible cells in the list.
ListCell
created by a ListCellFactory
, to custom
the item representation of the list, you need a custom ListCellFactory
.
For example a IconListCellFactory create IconListCells.
ListCellFactory
is related to the List's performace too, see the doc
comments of ListCellFactory
for the details.
And if you want a horizontal scrollvar visible when item width is bigger than the visible
width, you need a not shareCells
Factory(and of course the List should located
in a JScrollPane first). shareCells
Factory
can not count the maximum width of list items.
new JList()
public function JList(listData:Object, cellFactory:ListCellFactory)
JList(listData:Array, cellFactory:ListCellFactory)
JList(model:ListModel, cellFactory:ListCellFactory)
JList(listData:Array)
JList(model:ListModel)
JList()
listData | a ListModel or a Array. |
cellFactory | the cellFactory for this List. |
static public ON_STATE_CHANGED:String
static public ON_SELECTION_CHANGE:String
static public ON_LIST_SCROLL:String
static public ON_ITEM_PRESS:String
static public ON_ITEM_RELEASE:String
static public ON_ITEM_RELEASEOUTSIDE:String
static public ON_ITEM_ROLLOVER:String
static public ON_ITEM_ROLLOUT:String
static public ON_ITEM_CLICKED:String
static public SINGLE_SELECTION:Number
static public MULTIPLE_SELECTION:Number
public function updateUI():Void
Description copied from Component
Resets the UI property to a value from the current look and feel.
Component
subclasses must override this method
like this:
public void updateUI() { setUI((SliderUI)UIManager.getUI(this); }
public function setUI(newUI:ListUI):Void
Description copied from Component
Sets the look and feel delegate for this component.
Component
subclasses generally override this method
to narrow the argument type. For example, in JSlider
:
public void setUI(SliderUI newUI) { super.setUI(newUI); }
Additionally Component
subclasses must provide a
getUI
method that returns the correct type. For example:
public SliderUI getUI() { return (SliderUI)ui; }
public function getUIClassID():String
Description copied from Component
Returns the UIDefaults
key used to
look up the name of the org.aswing.plaf.ComponentUI
class that defines the look and feel
for this component. Most applications will never need to
call this method. Subclasses of Component
that support
pluggable look and feel should override this method to
return a UIDefaults
key that maps to the
ComponentUI
subclass that defines their look and feel.
public function setLayout(layout:LayoutManager):Void
Can not set layout to JList, its layout is itself.
Error | when set any layout. |
public function setListData(ld:Array):Void
set a array to be the list data, but array is not a List Mode. So when the array content was changed, you should call updateListView to update the JList.But this is not a good way, its slow. So suggest you to create a ListMode ex VectorListMode to JList, When you modify ListMode, it will automatic update JList.
public function setCellFactory(newFactory:ListCellFactory):Void
This will cause all cells recreating by new factory.
newFactory | the new cell factory for this List |
public function getPreferredWidthWhenNoCount():Number
the default preferred with of the List when shareCelles
.
public function setPreferredWidthWhenNoCount(preferredWidthWhenNoCount:Number):Void
The preferred with of the List, it is only used when List have no counting for its prefferredWidth.
When ListCellFactory
is shareCelles
, List will not count prefferred width.
preferredWidthWhenNoCount | the preferred with of the List. |
public function updateListView():Void
When your list data changed, and you want to update list view by hand. call this method.
This method is called automatically when setModel called with a different model to set.
public function clearSelection():Void
Clears the selection - after calling this method isSelectionEmpty will return true. This is a convenience method that just delegates to the selectionModel.
public function setSelectionMode(sm:Number):Void
Determines whether single-item or multiple-item selections are allowed. If selection mode changed, will cause clear selection;
public function getSelectionMode():Number
Return whether single-item or multiple-item selections are allowed.
public function getSelectedIndex():Number
Return the selected index, if selection multiple, return the first. if not selected any, return -1.
the selected index
public function isSelectionEmpty():Boolean
Returns true if nothing is selected.
true if nothing is selected, false otherwise.
public function getSelectedIndices():Array
Returns an array of all of the selected indices in increasing order.
a array contains all selected indices
public function isSelectedIndex(index:Number):Boolean
true if the index is selected, otherwise false.
public function getSelectedValue():Object
Returns the first selected value, or null if the selection is empty.
the first selected value
public function getSelectedValues():Array
Returns an array of the values for the selected cells. The returned values are sorted in increasing index order.
the selected values or an empty list if nothing is selected
public function setSelectedIndex(index:Number):Void
Selects a single cell. This will cause ON_SELECTION_CHANGE event only if the new index is different from currently selection.
This will not cause a scroll, if you want to scroll to visible the selected value, call ensureIndexIsVisible().
index | the index of the one cell to select |
public function setSelectedIndices(indices:Array):Void
Selects a set of cells. This will cause ON_SELECTION_CHANGE event what ever the new
indices is different from current selection or not.
If new the selection mode is SINGAL_SELECTION
mode, just set the first index in indices.
This will not cause a scroll, if you want to scroll to visible the selected value, call ensureIndexIsVisible().
indices | an array of the indices of the cells to select |
public function setSelectedValue(value:Object):Void
Selects the specified object from the list. This will not cause a scroll, if you want to scroll to visible the selected value, call ensureIndexIsVisible().
public function ensureIndexIsVisible(index:Number):Void
Scrolls the JList to make the specified cell completely visible.
public function setFirstVisibleIndex(index:Number):Void
scroll the list to view the specified index as first visible. If the list data elements is too short can not move the specified index to be first, just scroll as top as can.
public function setLastVisibleIndex(index:Number):Void
scroll the list to view the specified index as last visible If the list data elements is too short can not move the specified index to be last, just scroll as bottom as can.
public function scrollToBottomLeft():Void
Scrolls to view bottom left content.
This will make the scrollbars of JScrollPane
scrolled automatically,
if it is located in a JScrollPane
.
public function scrollToBottomRight():Void
Scrolls to view bottom right content.
This will make the scrollbars of JScrollPane
scrolled automatically,
if it is located in a JScrollPane
.
public function scrollToTopLeft():Void
Scrolls to view top left content.
This will make the scrollbars of JScrollPane
scrolled automatically,
if it is located in a JScrollPane
.
public function scrollToTopRight():Void
Scrolls to view to right content.
This will make the scrollbars of JScrollPane
scrolled automatically,
if it is located in a JScrollPane
.
public function setEnabled(b:Boolean):Void
Enables the list so that items can be selected.
public function getVerticalUnitIncrement():Number
Description copied from Viewportable
Returns the unit value for the Vertical scrolling.
public function getVerticalBlockIncrement():Number
Description copied from Viewportable
Return the block value for the Vertical scrolling.
public function getHorizontalUnitIncrement():Number
Description copied from Viewportable
Returns the unit value for the Horizontal scrolling.
public function getHorizontalBlockIncrement():Number
Description copied from Viewportable
Return the block value for the Horizontal scrolling.
public function setViewportTestSize(s:Dimension):Void
Description copied from Viewportable
Before JScrollPane analyse the scroll properties(call getExtentSize and getViewSize), it will call this method to set the size of viewport will be to test.
public function getExtentSize():Dimension
Description copied from Viewportable
Returns the size of the visible part of the view in view logic coordinates.
public function getViewSize():Dimension
Description copied from Viewportable
Returns the viewportable view's amount size if view all content in view logic coordinates. Usually the view's preffered size.
public function getViewPosition():Point
Description copied from Viewportable
Returns the view coordinates that appear in the upper left hand corner of the viewport, or 0,0 if there's no view. in view logic coordinates.
public function setViewPosition(p:Point):Void
Description copied from Viewportable
Sets the view coordinates that appear in the upper left hand corner of the viewport. in view logic coordinates.
public function scrollRectToVisible(contentRect:Rectangle):Void
Description copied from Viewportable
Scrolls the view so that Rectangle
within the view becomes visible. in view logic coordinates.
Note that this method will not scroll outside of the
valid viewport; for example, if contentRect
is larger
than the viewport, scrolling will be confined to the viewport's
bounds.
public function addChangeListener(func:Function, obj:Object):Object
Description copied from Viewportable
Add a listener to listen the viewpoat state change event.
When the viewpoat's state changed, the state is all about:
public function getViewportPane():Component
Description copied from Viewportable
Return the component of the viewportable's pane which would added to displayed on the stage.
public function preferredLayoutSize(target:Container):Dimension
Description copied from LayoutManager
Calculates the preferred size dimensions for the specified container, given the components it contains.
public function minimumLayoutSize(target:Container):Dimension
Description copied from LayoutManager
Calculates the minimum size dimensions for the specified container, given the components it contains.
public function maximumLayoutSize(target:Container):Dimension
Description copied from LayoutManager
Calculates the maximum size dimensions for the specified container, given the components it contains.
public function invalidateLayout(target:Container):Void
Description copied from LayoutManager
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
public function intervalAdded(e:ListDataEvent):Void
data in list has changed, update JList if needed.
public function intervalRemoved(e:ListDataEvent):Void
data in list has changed, update JList if needed.
public function contentsChanged(e:ListDataEvent):Void
data in list has changed, update JList if needed.