|
JavaTM 2 Platform Standard Ed. 5.0 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JList
public class JList
该组件允许用户从列表中选择一个或多个对象。单独的模型 ListModel 表示列表的内容。使用构建 ListModel 实例的 JList 构造方法,可以方便地显示对象的数组或向量:
// Create a JList that displays the strings in data[]
String[] data = {"one", "two", "three", "four"};
JList dataList = 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 superclass of JList.class.
// We store the superclasses in a java.util.Vector.
Vector superClasses = new Vector();
Class rootClass = javax.swing.JList.class;
for(Class cls = rootClass; cls != null; cls = cls.getSuperclass()) {
superClasses.addElement(cls);
}
JList classList = new JList(superClasses);
JList 不支持直接滚动。要创建滚动列表,需要让 JList 作为 JScrollPane 的视口视图。例如:
JScrollPane scrollPane = new JScrollPane(dataList); // Or in two steps: JScrollPane scrollPane = new JScrollPane(); scrollPane.getViewport().setView(dataList);
默认情况下,JList 选择模型允许使用常量 MULTIPLE_INTERVAL_SELECTION 一次选择任何项的组合。选择状态由单独的委托对象(即 ListSelectionModel 的实例)实际管理。不过,JList 提供了便捷的属性,可用于管理选择。
String[] data = {"one", "two", "three", "four"};
JList dataList = new JList(data);
dataList.setSelectedIndex(1); // select "two"
dataList.getSelectedValue(); // returns "two"
JList 的内容可以是动态的,换句话说,在创建 JList 之后,列表元素可以改变值,列表的大小也可以改变。JList 利用 swing.event.ListDataListener 实现在其模型中观察更改。正确实现的 ListModel 在每次发生更改时向其侦听器发出通知。更改的特征由标识已修改、已添加或已移除的列表索引范围的 swing.event.ListDataEvent 来描述。简单动态内容 JList 应用程序可以使用 DefaultListModel 类存储列表元素。此类实现 ListModel 接口,同时提供 java.util.Vector API。需要提供自定义 ListModel 实现的应用程序可以为提供基本 ListDataListener 支持的 AbstractListModel 创建子类。例如:
// This list model has about 2^16 elements. Enjoy scrolling. ListModel bigData = new AbstractListModel() { public int getSize() { return Short.MAX_VALUE; } public Object getElementAt(int index) { return "Index " + index; } }; JList bigDataList = new JList(bigData); // We don't want the JList implementation to compute the width // or height of all of the list cells, so we give it a string // that's as big as we'll need for any cell. It uses this to // compute values for the fixedCellWidth and fixedCellHeight // properties. bigDataList.setPrototypeCellValue("Index 1234567890");
JList 使用 java.awt.Component(由名为 cellRendererer 的委派提供)在列表中绘制可见单元。单元渲染器组件类似于“橡皮图章”,用于绘制每个可见行。每当 JList 需要绘制单元时,它就要求单元渲染器提供组件,使用 setBounds() 将其移动到位,然后通过调用其绘制方法来绘制。默认的单元渲染器使用 JLabel 组件呈现每个组件的字符串值。用户还可以使用如下代码替代自己的单元渲染器:
// Display an icon and a string for each object in the list. class MyCellRenderer extends JLabel implements ListCellRenderer { final static ImageIcon longIcon = new ImageIcon("long.gif"); final static ImageIcon shortIcon = new ImageIcon("short.gif"); // This is the only method defined by ListCellRenderer. // We just reconfigure the JLabel each time we're called. public Component getListCellRendererComponent( JList list, Object value, // value to display int index, // cell index boolean isSelected, // is the cell selected boolean cellHasFocus) // the list and the cell have the focus { String s = value.toString(); setText(s); setIcon((s.length() > 10) ? longIcon : shortIcon); if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } setEnabled(list.isEnabled()); setFont(list.getFont()); setOpaque(true); return this; } } String[] data = {"one", "two", "three", "four"}; JList dataList = new JList(data); dataList.setCellRenderer(new MyCellRenderer());
JList 没有对处理两次或三次(或 N 次)鼠标单击提供特殊支持,不过可以使用 MouseListener 方便地处理这些操作。使用 JList 方法 locationToIndex() 确定单击的是哪一个单元。例如:
final JList list = new JList(dataModel);
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
System.out.println("Double clicked on Item " + index);
}
}
};
list.addMouseListener(mouseListener);
注意,在此例中,dataList 为 final,因为它由匿名 MouseListener 类引用。
警告:此类的序列化对象将与以后的 Swing 版本不兼容。当前的序列化支持适用于短期存储或运行相同 Swing 版本的应用程序之间的 RMI。从 1.4 版本开始,已在 java.beans 包中添加了支持所有 JavaBeansTM 长期存储的功能。请参见 XMLEncoder。
请参见《The Java Tutorial》中的 How to Use Lists 以获取更多文档。另请参见 The Swing Connection 中的文章 Advanced JList Programming。
ListModel,
AbstractListModel,
DefaultListModel,
ListSelectionModel,
DefaultListSelectionModel,
ListCellRenderer| 嵌套类摘要 | |
|---|---|
protected class |
JList.AccessibleJList
此类实现 JList 类的可访问性支持。 |
| 从类 javax.swing.JComponent 继承的嵌套类/接口 |
|---|
JComponent.AccessibleJComponent |
| 从类 java.awt.Container 继承的嵌套类/接口 |
|---|
Container.AccessibleAWTContainer |
| 从类 java.awt.Component 继承的嵌套类/接口 |
|---|
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy |
| 字段摘要 | |
|---|---|
static int |
HORIZONTAL_WRAP
指示“报纸样式”,单元按先横向后纵向流动。 |
static int |
VERTICAL
指示默认布局:一列单元。 |
static int |
VERTICAL_WRAP
指示“报纸样式”布局,单元按先纵向后横向流动。 |
| 从类 javax.swing.JComponent 继承的字段 |
|---|
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
| 从类 java.awt.Component 继承的字段 |
|---|
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
| 从接口 java.awt.image.ImageObserver 继承的字段 |
|---|
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
| 构造方法摘要 | |
|---|---|
JList()
构造一个使用空模型的 JList。 |
|
JList(ListModel dataModel)
构造一个 JList,使其使用指定的非 null 模型显示元素。 |
|
JList(Object[] listData)
构造一个 JList,使其显示指定数组中的元素。 |
|
JList(Vector<?> listData)
构造一个 JList,使其显示指定 Vector 中的元素。 |
|
| 方法摘要 | |
|---|---|
void |
addListSelectionListener(ListSelectionListener listener)
为每次选择发生更改时要通知的列表添加侦听器。 |
void |
addSelectionInterval(int anchor,
int lead)
将选择设置为指定间隔与当前选择的并集。 |
void |
clearSelection()
清除选择,调用此方法后, isSelectionEmpty 将返回 true。 |
protected ListSelectionModel |
createSelectionModel()
返回 DefaultListSelectionModel 实例。 |
void |
ensureIndexIsVisible(int index)
滚动视口,使指定单元完全可见。 |
protected void |
fireSelectionValueChanged(int firstIndex,
int lastIndex,
boolean isAdjusting)
通知 JList ListSelectionListener 选择模型已改变。 |
AccessibleContext |
getAccessibleContext()
获取与此 JList 关联的 AccessibleContext。 |
int |
getAnchorSelectionIndex()
返回最近一次 addSelectionModel 或 setSelectionInterval 调用中的第一个 index 参数。 |
Rectangle |
getCellBounds(int index0,
int index1)
返回 JList 坐标中项的指定范围的边界。 |
ListCellRenderer |
getCellRenderer()
返回呈现列表项的对象。 |
boolean |
getDragEnabled()
获取 dragEnabled 属性。 |
int |
getFirstVisibleIndex()
返回第一个可见单元的索引。 |
int |
getFixedCellHeight()
返回固定单元高度值,该值通过设置 fixedCellHeight 属性指定,而不是根据列表元素计算。 |
int |
getFixedCellWidth()
返回固定单元宽度值,该值通过设置 fixedCellWidth 属性指定,而不是根据列表元素计算。 |
int |
getLastVisibleIndex()
返回最后一个可见单元的索引。 |
int |
getLayoutOrientation()
如果布局是单列单元,则返回 JList.VERTICAL;如果布局是“报纸样式”并且内容按先纵向后横向流动,则返回 JList.VERTICAL_WRAP;如果布局是“报纸样式”并且内容按先横向后纵向流动,则返回 JList.HORIZONTAL_WRAP。 |
int |
getLeadSelectionIndex()
返回最近一次 addSelectionInterval 或 setSelectionInterval 调用中的第二个 index 参数。 |
ListSelectionListener[] |
getListSelectionListeners()
返回使用 addListSelectionListener() 添加到此 JList 中的所有 ListSelectionListener 组成的数组。 |
int |
getMaxSelectionIndex()
返回选择的最大单元索引。 |
int |
getMinSelectionIndex()
返回选择的最小单元索引。 |
ListModel |
getModel()
返回保存由 JList 组件显示的项列表的数据模型。 |
int |
getNextMatch(String prefix,
int startIndex,
Position.Bias bias)
返回以某一前缀开头的下一个列表元素。 |
Dimension |
getPreferredScrollableViewportSize()
计算显示 visibleRowCount 行所需的视口的大小。 |
Object |
getPrototypeCellValue()
返回“原型单元”的单元宽度,原型单元是一个用于计算单元宽度的单元,因为它与所有其他列表项具有相同的值。 |
int |
getScrollableBlockIncrement(Rectangle visibleRect,
int orientation,
int direction)
返回为显露上一个或下一个块而滚动的距离。 |
boolean |
getScrollableTracksViewportHeight()
如果此 JList 在 JViewport 中显示并且视口的高度大于 JList 的首选高度,或者布局方向为 VERTICAL_WRAP 但可见行数 <= 0,则返回 true;否则返回 false。 |
boolean |
getScrollableTracksViewportWidth()
如果此 JList 在 JViewport 中显示并且视口的宽度大于 JList 的首选宽度,或者布局方向为 HORIZONTAL_WRAP 但可见行数 <= 0,则返回 true;否则返回 false。 |
int |
getScrollableUnitIncrement(Rectangle visibleRect,
int orientation,
int direction)
返回为显露上一个或下一个行(纵向滚动)或列(横向滚动)而滚动的距离。 |
int |
getSelectedIndex()
返回所选的第一个索引;如果没有选择项,则返回 -1。 |
int[] |
getSelectedIndices()
返回所选的全部索引的数组(按升序排列)。 |
Object |
getSelectedValue()
返回所选的第一个值,如果选择为空,则返回 null。 |
Object[] |
getSelectedValues()
返回所选单元的一组值。 |
Color |
getSelectionBackground()
返回所选单元的背景色。 |
Color |
getSelectionForeground()
返回选择的前景色。 |
int |
getSelectionMode()
返回允许单项选择还是多项选择。 |
ListSelectionModel |
getSelectionModel()
返回当前选择模型的值。 |
String |
getToolTipText(MouseEvent event)
重写 JComponent 的 getToolTipText 方法,从而允许使用渲染器的提示(如果设置了文本)。 |
ListUI |
getUI()
返回呈现此组件的外观 (L&F) 对象。 |
String |
getUIClassID()
返回用于构造呈现此组件时使用的外观 (L&F) 类名称的后缀。 |
boolean |
getValueIsAdjusting()
返回数据模型的 isAdjusting 属性的值。 |
int |
getVisibleRowCount()
返回首选可见行数。 |
Point |
indexToLocation(int index)
返回 JList 坐标中指定项的原点。 |
boolean |
isSelectedIndex(int index)
如果选择了指定的索引,则返回 true。 |
boolean |
isSelectionEmpty()
如果什么也没有选择,则返回 true。 |
int |
locationToIndex(Point location)
将 JList 坐标中的点转换为该处单元最接近的索引。 |
protected String |
paramString()
返回此 JList 的字符串表示形式。 |
void |
removeListSelectionListener(ListSelectionListener listener)
从每次选择发生更改时要通知的列表中移除侦听器。 |
void |
removeSelectionInterval(int index0,
int index1)
将选择设置为指定间隔和当前选择的差集。 |
void |
setCellRenderer(ListCellRenderer cellRenderer)
设置用于绘制列表中每个单元的委托。 |
void |
setDragEnabled(boolean b)
设置 dragEnabled 属性,该属性必须为 true 才能启用对此组件的自动拖动处理(拖放操作的第一部分)。 |
void |
setFixedCellHeight(int height)
设置列表中每个单元的高度。 |
void |
setFixedCellWidth(int width)
设置列表中每个单元的宽度。 |
void |
setLayoutOrientation(int layoutOrientation)
定义布置列表单元的方式。 |
void |
setListData(Object[] listData)
根据一个 object 数组构造 ListModel,然后对其应用 setModel。 |
void |
setListData(Vector<?> listData)
根据 Vector 构造 ListModel,然后对其应用 setModel。 |
void |
setModel(ListModel model)
设置表示列表内容或“值”的模型,并在通知 PropertyChangeListener 之后清除列表选择。 |
void |
setPrototypeCellValue(Object prototypeCellValue)
计算 fixedCellWidth 和 fixedCellHeight 属性,方法是针对指定值将 cellRenderer 配置为索引等于零,然后计算渲染器组件的首选大小。 |
void |
setSelectedIndex(int index)
选择单个单元。 |
void |
setSelectedIndices(int[] indices)
选择一组单元。 |
void |
setSelectedValue(Object anObject,
boolean shouldScroll)
从列表中选择指定的对象。 |
void |
setSelectionBackground(Color selectionBackground)
设置所选单元的背景色。 |
void |
setSelectionForeground(Color selectionForeground)
设置所选单元的前景色。 |
void |
setSelectionInterval(int anchor,
int lead)
选择指定的间隔。 |
void |
setSelectionMode(int selectionMode)
确定允许单项选择还是多项选择。 |
void |
setSelectionModel(ListSelectionModel selectionModel)
将列表的 selectionModel 设置为非 null 的 ListSelectionModel 实现。 |
void |
setUI(ListUI ui)
设置呈现此组件的外观 (L&F) 对象。 |
void |
setValueIsAdjusting(boolean b)
将数据模型的 isAdjusting 属性设置为 true,这样完成所有选择事件时就会生成单个事件(例如,在选择模式的列表上拖动鼠标时)。 |
void |
setVisibleRowCount(int visibleRowCount)
设置不使用滚动条可以在列表中显示的首选行数,这一点由最近的 JViewport 祖先(如果有)确定。 |
void |
updateUI()
根据当前外观的值重置 UI 属性。 |
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| 字段详细信息 |
|---|
public static final int VERTICAL
setLayoutOrientation(int),
常量字段值public static final int VERTICAL_WRAP
setLayoutOrientation(int),
常量字段值public static final int HORIZONTAL_WRAP
setLayoutOrientation(int),
常量字段值| 构造方法详细信息 |
|---|
public JList(ListModel dataModel)
JList,使其使用指定的非 null 模型显示元素。所有 JList 构造方法都委派给此方法。
dataModel - 此列表的数据模型
IllegalArgumentException - 如果 dataModel 为 nullpublic JList(Object[] listData)
JList,使其显示指定数组中的元素。此构造方法仅委派给 ListModel 构造方法。
listData - 要加载到数据模型中的 Object 的数组public JList(Vector<?> listData)
JList,使其显示指定 Vector 中的元素。此构造方法仅委派给 ListModel 构造方法。
listData - 要加载到数据模型中的 Vectorpublic JList()
JList。
| 方法详细信息 |
|---|
public ListUI getUI()
ListUI 对象public void setUI(ListUI ui)
ui - ListUI L&F 对象UIDefaults.getUI(javax.swing.JComponent)public void updateUI()
JComponent 中的 updateUIpublic String getUIClassID()
JComponent 中的 getUIClassIDJComponent.getUIClassID(),
UIDefaults.getUI(javax.swing.JComponent)public Object getPrototypeCellValue()
prototypeCellValue 属性的值setPrototypeCellValue(java.lang.Object)public void setPrototypeCellValue(Object prototypeCellValue)
fixedCellWidth 和 fixedCellHeight 属性,方法是针对指定值将 cellRenderer 配置为索引等于零,然后计算渲染器组件的首选大小。当由于列表过长而不允许 JList 计算每个单元的宽度/高度,并且已知某个单元值所占用的空间与任何其他单元一样多时,这些属性非常有用。
注意,我们在此确实设置了 fixedCellWidth 和 fixedCellHeight 属性,但是仅激发了一个 prototypeCellValue PropertyChangeEvent。
要查看设置此属性的示例,请参见上述类描述。
此属性的默认值为 null。
这是一个 JavaBeans bound 属性。
prototypeCellValue - 作为 fixedCellWidth 和 fixedCellHeight 的基础的值getPrototypeCellValue(),
setFixedCellWidth(int),
setFixedCellHeight(int),
Container.addPropertyChangeListener(java.beans.PropertyChangeListener)public int getFixedCellWidth()
fixedCellWidth 属性指定,而不是根据列表元素计算。
setFixedCellWidth(int)public void setFixedCellWidth(int width)
width 为 -1,则可以通过将 getPreferredSize 应用到 cellRenderer 组件为每个列表元素计算单元宽度。
此属性的默认值为 -1。
这是一个 JavaBeans bound 属性。
width - 此列表中所有单元的宽度(以像素表示)getPrototypeCellValue(),
setFixedCellWidth(int),
Container.addPropertyChangeListener(java.beans.PropertyChangeListener)public int getFixedCellHeight()
fixedCellHeight 属性指定,而不是根据列表元素计算。
setFixedCellHeight(int)public void setFixedCellHeight(int height)
height 为 -1,则可以通过将 getPreferredSize 应用到 cellRenderer 组件为每个列表元素计算单元高度。
此属性的默认值为 -1。
这是一个 JavaBeans bound 属性。
height - 给出此列表中所有单元高度的整数(以像素表示)getPrototypeCellValue(),
setFixedCellWidth(int),
Container.addPropertyChangeListener(java.beans.PropertyChangeListener)public ListCellRenderer getCellRenderer()
ListCellRenderersetCellRenderer(javax.swing.ListCellRenderer)public void setCellRenderer(ListCellRenderer cellRenderer)
prototypeCellValue,则同时设置了 fixedCellWidth 和 fixedCellHeight 属性。但是仅对 cellRenderer 属性生成一个 PropertyChangeEvent。
此属性的默认值由 ListUI 委托(即外观实现)提供。
要查看设置单元渲染器的示例,请参见上述类描述。
这是一个 JavaBeans bound 属性。
cellRenderer - 绘制列表单元的 ListCellRenderergetCellRenderer()public Color getSelectionForeground()
Color 对象setSelectionForeground(java.awt.Color),
setSelectionBackground(java.awt.Color)public void setSelectionForeground(Color selectionForeground)
此属性的默认值由外观实现定义。
这是一个 JavaBeans bound 属性。
selectionForeground - 在所选列表项的前景中使用的 ColorgetSelectionForeground(),
setSelectionBackground(java.awt.Color),
JComponent.setForeground(java.awt.Color),
JComponent.setBackground(java.awt.Color),
JComponent.setFont(java.awt.Font)public Color getSelectionBackground()
ColorsetSelectionBackground(java.awt.Color),
setSelectionForeground(java.awt.Color)public void setSelectionBackground(Color selectionBackground)
此属性的默认值由外观实现定义。
这是一个 JavaBeans bound 属性。
selectionBackground - 用于所选单元的背景的 ColorgetSelectionBackground(),
setSelectionForeground(java.awt.Color),
JComponent.setForeground(java.awt.Color),
JComponent.setBackground(java.awt.Color),
JComponent.setFont(java.awt.Font)public int getVisibleRowCount()
setVisibleRowCount(int)public void setVisibleRowCount(int visibleRowCount)
JViewport 祖先(如果有)确定。此属性的值仅对 JList 的 preferredScrollableViewportSize 的值有影响。
此属性的默认值为 8。
这是一个 JavaBeans bound 属性。
visibleRowCount - 指定首选可见行数的整数getVisibleRowCount(),
JComponent.getVisibleRect(),
JViewportpublic int getLayoutOrientation()
JList.VERTICAL;如果布局是“报纸样式”并且内容按先纵向后横向流动,则返回 JList.VERTICAL_WRAP;如果布局是“报纸样式”并且内容按先横向后纵向流动,则返回 JList.HORIZONTAL_WRAP。
setLayoutOrientation(int)public void setLayoutOrientation(int layoutOrientation)
JList,它的布局可以采用以下方式之一:
0 1 2 3
0 1 2 3
0 2 1 3
它们与以下值相对应:
值 | 描述 |
|---|---|
JList.VERTICAL
| 应该在一个列中纵向布置单元。 |
JList.HORIZONTAL_WRAP
| 应该横向布置单元,需要时将单元包装到新行中。要使用的行数或者由 getVisibleRowCount 定义(如果大于 0)或者由 JList 的宽度确定。
|
JList.VERTICAL_WRAP
| 应该纵向布置单元,需要时将单元包装到新列中。要使用的行数或者由 getVisibleRowCount 定义(如果大于 0)或者由 JList 的高度确定。
|
JList.VERTICAL。
如果 layoutOrientation 不为 JList.HORIZONTAL_WRAP、JList.VERTICAL 或 JList.VERTICAL_WRAP 之一,则此操作抛出 IllegalArgumentException。
layoutOrientation - 新方向,JList.HORIZONTAL_WRAP、JList.VERTICAL 或 JList.VERTICAL_WRAP 之一。getLayoutOrientation(),
setVisibleRowCount(int),
getScrollableTracksViewportHeight()public int getFirstVisibleIndex()
componentOrientation 属性。如果方向为横向从左到右,则第一个可见单元位于列表的左上角。如果方向为横向从右到左,则第一个可见单元位于列表的右上角。如果任何单元都不可见或者列表为空,则返回 -1。注意,可返回仅部分可见的单元。
getLastVisibleIndex(),
JComponent.getVisibleRect()public int getLastVisibleIndex()
componentOrientation 属性。如果方向为横向从左到右,则最后一个可见单元位于 JList 的右下角。如果方向为横向从右到左,则最后一个可见单元位于 JList 的左下角。如果任何单元都不可见或者列表为空,则返回 -1。注意,可返回仅部分可见的单元。
getFirstVisibleIndex(),
JComponent.getVisibleRect()public void ensureIndexIsVisible(int index)
JList 必须在 JViewport 中显示。
index - 要变得可见的单元的索引JComponent.scrollRectToVisible(java.awt.Rectangle),
JComponent.getVisibleRect()public void setDragEnabled(boolean b)
dragEnabled 属性,该属性必须为 true 才能启用对此组件的自动拖动处理(拖放操作的第一部分)。必须将 transferHandler 属性设置为非 null 值,拖动才有效。dragEnabled 属性的默认值为 false。
启用自动拖动处理时,只要用户在选择上按下鼠标按键,然后将鼠标移动几个像素,大多数外观就开始拖放操作了。因此,将此属性设置为 true 可以对选择的行为方式产生微妙的影响。
有些外观可能不支持自动拖放;它们将忽略此属性。对于这些外观,可通过修改组件以直接调用 TransferHandler 的 exportAsDrag 方法来处理该问题。
b - 作为 dragEnabled 属性设置目标的值
HeadlessException - 如果 b 为 true 并且 GraphicsEnvironment.isHeadless() 返回 trueGraphicsEnvironment.isHeadless(),
getDragEnabled(),
JComponent.setTransferHandler(javax.swing.TransferHandler),
TransferHandlerpublic boolean getDragEnabled()
dragEnabled 属性。
dragEnabled 属性的值setDragEnabled(boolean)public int getNextMatch(String prefix, int startIndex, Position.Bias bias)
prefix - 要测试是否匹配的字符串startIndex - 开始搜索的索引bias - 搜索方向,Position.Bias.Forward 或 Position.Bias.Backward。
IllegalArgumentException - 如果 prefix 为 null 或者 startIndex 超出范围public String getToolTipText(MouseEvent event)
JComponent 的 getToolTipText 方法,从而允许使用渲染器的提示(如果设置了文本)。
JList 正确显示渲染器的工具提示,JList 必须是 ToolTipManager 中的注册组件。此操作可以在构造方法中自动完成,但是如果之后在 JList 上调用了 setToolTipText(null),则这是一个注销的列表组件,渲染器的提示将不再显示。
JComponent 中的 getToolTipTextpublic int locationToIndex(Point location)
JList 坐标中的点转换为该处单元最接近的索引。要确定单元是否实际包含指定的位置,需要配合使用此方法与 getCellBounds。如果模型为空,则返回 -1。
location - 相对于 JList 的单元坐标
public Point indexToLocation(int index)
JList 坐标中指定项的原点。如果 index 无效,则返回 null。
index - JList 单元的索引
public Rectangle getCellBounds(int index0, int index1)
JList 坐标中项的指定范围的边界。如果索引无效,则返回 null。
index0 - 该范围中第一个 JList 单元的索引index1 - 该范围中最后一个 JList 单元的索引
public ListModel getModel()
JList 组件显示的项列表的数据模型。
ListModelsetModel(javax.swing.ListModel)public void setModel(ListModel model)
PropertyChangeListener 之后清除列表选择。
这是一个 JavaBeans bound 属性。
model - 提供要显示的项列表的 ListModel
IllegalArgumentException - 如果 model 为 nullgetModel()public void setListData(Object[] listData)
ListModel,然后对其应用 setModel。
listData - 包含要在列表中显示的项的 Object 数组setModel(javax.swing.ListModel)public void setListData(Vector<?> listData)
Vector 构造 ListModel,然后对其应用 setModel。
listData - 包含要在列表中显示的项的 VectorsetModel(javax.swing.ListModel)protected ListSelectionModel createSelectionModel()
DefaultListSelectionModel 实例。此方法供构造方法使用,用来初始化 selectionModel 属性。
JList 所使用的 ListSelectionModel。setSelectionModel(javax.swing.ListSelectionModel),
DefaultListSelectionModelpublic ListSelectionModel getSelectionModel()
ListSelectionModelsetSelectionModel(javax.swing.ListSelectionModel),
ListSelectionModel
protected void fireSelectionValueChanged(int firstIndex,
int lastIndex,
boolean isAdjusting)
JList ListSelectionListener 选择模型已改变。用于将 ListSelectionEvent 从 selectionModel 转发到直接添加到 JList 的 ListSelectionListener。
firstIndex - 选择的第一个索引lastIndex - 选择的最后一个索引isAdjusting - 如果要进行多处更改则为 trueaddListSelectionListener(javax.swing.event.ListSelectionListener),
removeListSelectionListener(javax.swing.event.ListSelectionListener),
EventListenerListpublic void addListSelectionListener(ListSelectionListener listener)
JList 的侦听器将满足:ListSelectionEvent.getSource() == this JList(而 ListSelectionModel)。
listener - 要添加的 ListSelectionListenergetSelectionModel(),
getListSelectionListeners()public void removeListSelectionListener(ListSelectionListener listener)
listener - 要移除的 ListSelectionListenergetSelectionModel(),
getListSelectionListeners()public ListSelectionListener[] getListSelectionListeners()
ListSelectionListener 组成的数组。
ListSelectionListener;如果没有添加任何侦听器,则返回空数组addListSelectionListener(javax.swing.event.ListSelectionListener)public void setSelectionModel(ListSelectionModel selectionModel)
selectionModel 设置为非 null 的 ListSelectionModel 实现。选择模型可以完成单个选择、连续范围选择和非连续范围选择等任务。
这是一个 JavaBeans bound 属性。
selectionModel - 实现选择的 ListSelectionModel
IllegalArgumentException - 如果 selectionModel 为 nullgetSelectionModel()public void setSelectionMode(int selectionMode)
selectionMode 值:
ListSelectionModel.SINGLE_SELECTION 一次只能选择一个列表索引。在此模式中,setSelectionInterval 方法等价于只使用第二个 index 参数的 addSelectionInterval 方法。
ListSelectionModel.SINGLE_INTERVAL_SELECTION 一次可以选择一个连续的索引间隔。在此模式中,setSelectionInterval 等价于 addSelectionInterval。
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION 在此模式中,不存在对选择的限制。这是默认设置。
selectionMode - 指定可能的选择类型的整数getSelectionMode()public int getSelectionMode()
selectionMode 属性的值setSelectionMode(int)public int getAnchorSelectionIndex()
addSelectionModel 或 setSelectionInterval 调用中的第一个 index 参数。这是一个仅委托给 selectionModel 的便捷方法。
public int getLeadSelectionIndex()
addSelectionInterval 或 setSelectionInterval 调用中的第二个 index 参数。这是一个仅委托给 selectionModel 的便捷方法。
public int getMinSelectionIndex()
selectionModel 的便捷方法。
public int getMaxSelectionIndex()
selectionModel 的便捷方法。
public boolean isSelectedIndex(int index)
selectionModel 的便捷方法。
index - 要查询其选择状态的索引
ListSelectionModel.isSelectedIndex(int),
setSelectedIndex(int),
addListSelectionListener(javax.swing.event.ListSelectionListener)public boolean isSelectionEmpty()
selectionModel 的便捷方法。
ListSelectionModel.isSelectionEmpty(),
clearSelection(),
addListSelectionListener(javax.swing.event.ListSelectionListener)public void clearSelection()
isSelectionEmpty 将返回 true。这是一个仅委托给 selectionModel 的便捷方法。
public void setSelectionInterval(int anchor,
int lead)
anchor 和 lead 两个索引。没有必要让 anchor 小于 lead。这是一个仅委托给 selectionModel 的便捷方法。如果 anchor 或 lead 为 -1,则 DefaultListSelectionModel 实现不执行任何操作。如果 anchor 或 lead 小于 -1,则抛出 IndexOutOfBoundsException。
anchor - 要选择的第一个索引lead - 要选择的最后一个索引
IndexOutOfBoundsException - 如果 anchor 或 lead 小于 -1ListSelectionModel.setSelectionInterval(int, int),
addSelectionInterval(int, int),
removeSelectionInterval(int, int),
addListSelectionListener(javax.swing.event.ListSelectionListener)
public void addSelectionInterval(int anchor,
int lead)
selectionModel 的便捷方法。如果 anchor 或 lead 为 -1,则 DefaultListSelectionModel 实现不执行任何操作。如果 anchor 或 lead 小于 -1,则抛出 IndexOutOfBoundsException。
anchor - 要添加到选择的第一个索引lead - 要添加到选择的最后一个索引
IndexOutOfBoundsException - 如果 anchor 或 lead 小于 -1ListSelectionModel.addSelectionInterval(int, int),
setSelectionInterval(int, int),
removeSelectionInterval(int, int),
addListSelectionListener(javax.swing.event.ListSelectionListener)
public void removeSelectionInterval(int index0,
int index1)
index0 和 index1 都要移除。没有必要让 index0 小于 index1。这是一个仅委托给 selectionModel 的便捷方法。如果 index0 或 index1 为 -1,则 DefaultListSelectionModel 实现不执行任何操作。如果 index0 或 index1 小于 -1,则抛出 IndexOutOfBoundsException。
index0 - 要从选择移除的第一个索引index1 - 要从选择移除的最后一个索引
IndexOutOfBoundsException - 如果 index0 或 index1 小于 -1ListSelectionModel.removeSelectionInterval(int, int),
setSelectionInterval(int, int),
addSelectionInterval(int, int),
addListSelectionListener(javax.swing.event.ListSelectionListener)public void setValueIsAdjusting(boolean b)
isAdjusting 属性设置为 true,这样完成所有选择事件时就会生成单个事件(例如,在选择模式的列表上拖动鼠标时)。
b - 属性值的 boolean 值ListSelectionModel.setValueIsAdjusting(boolean)public boolean getValueIsAdjusting()
isAdjusting 属性的值。如果进行了多处更改,则此值为 true。
ListSelectionModel.getValueIsAdjusting()public int[] getSelectedIndices()
public void setSelectedIndex(int index)
public void setSelectedIndices(int[] indices)
public Object[] getSelectedValues()
isSelectedIndex(int),
getModel(),
addListSelectionListener(javax.swing.event.ListSelectionListener)public int getSelectedIndex()
getMinSelectionIndex 的值getMinSelectionIndex(),
addListSelectionListener(javax.swing.event.ListSelectionListener)public Object getSelectedValue()
null。
public void setSelectedValue(Object anObject, boolean shouldScroll)
anObject - 要选择的对象shouldScroll - 如果所选对象存在,但列表需要滚动才能显示,则为 true;否则为 falsepublic Dimension getPreferredScrollableViewportSize()
visibleRowCount 行所需的视口的大小。如果指定了 fixedCellWidth 和 fixedCellHeight,则此操作无足轻重。注意,可以使用 prototypeCellValue 属性隐式指定它们。如果未指定 fixedCellWidth,则通过查找最宽的列表元素来计算。如果未指定 fixedCellHeight,则我们采取直观推断:
visibleRowCount 即可。
JList.getModel().getSize() == 0),则为每个可见行分配 16 个像素,为其宽度分配 256 个像素(除非设置了 fixedCellWidth),即可获取最佳效果。
VERTICAL,则此操作将返回 getPreferredSize 的返回值。当前的 ListUI 需要重写 getPreferredSize 才能返回适当的值。
Scrollable 中的 getPreferredScrollableViewportSizevisibleRowCount 行所需视口大小的 dimensiongetPreferredScrollableViewportSize(),
setPrototypeCellValue(java.lang.Object)public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
对于横向滚动,如果列表是纵向布置的(即 orientation 为 VERTICAL),则返回列表字体大小或 1(如果使用了为 null 的字体)。
注意,visibleRect 的值必须等于 this.getVisibleRect()。
Scrollable 中的 getScrollableUnitIncrementvisibleRect - 可见矩形orientation - HORIZONTAL 或 VERTICALdirection - 如果 <= 0,则向上滚动;如果 > 0,则向下滚动
IllegalArgumentException - 如果 visibleRect 为 null 或者 orientation 不为 SwingConstants.VERTICAL 或 SwingConstants.HORIZONTAL 之一。Scrollable.getScrollableUnitIncrement(java.awt.Rectangle, int, int)public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
direction 大于 0),则最后一个可见元素应该成为第一个完全可见元素
对于横向滚动,如果列表是横向布置的(orientation 为 VERTICAL_WRAP 或 HORIZONTAL_WRAP):
direction 大于 0),则最后一个可见元素应该成为第一个完全可见元素
如果纵向布置列表,则返回 visibleRect.width。
注意,visibleRect 的值必须等于 this.getVisibleRect()。
Scrollable 中的 getScrollableBlockIncrementvisibleRect - 可见矩形orientation - HORIZONTAL 或 VERTICALdirection - 如果 <= 0,则向上滚动;如果 > 0,则向下滚动
IllegalArgumentException - 如果 visibleRect 为 null 或者 orientation 不为 SwingConstants.VERTICAL 或 SwingConstants.HORIZONTAL 之一。Scrollable.getScrollableUnitIncrement(java.awt.Rectangle, int, int)public boolean getScrollableTracksViewportWidth()
JList 在 JViewport 中显示并且视口的宽度大于 JList 的首选宽度,或者布局方向为 HORIZONTAL_WRAP 但可见行数 <= 0,则返回 true;否则返回 false。如果返回 false,则不跟踪视口宽度。如果 JViewport 本身嵌入在 JScrollPane 中,则此操作允许横向滚动。
Scrollable 中的 getScrollableTracksViewportWidthJList 的首选宽度,则返回 true;否则返回 falseScrollable.getScrollableTracksViewportWidth()public boolean getScrollableTracksViewportHeight()
JList 在 JViewport 中显示并且视口的高度大于 JList 的首选高度,或者布局方向为 VERTICAL_WRAP 但可见行数 <= 0,则返回 true;否则返回 false。如果返回 false,则不跟踪视口高度。如果 JViewport 本身嵌入在 JScrollPane 中,则此操作允许纵向滚动。
Scrollable 中的 getScrollableTracksViewportHeightJlist 的首选高度,则返回 true;否则返回 falseScrollable.getScrollableTracksViewportHeight()protected String paramString()
JList 的字符串表示形式。此方法仅在进行调试的时候使用,对于各个实现,所返回字符串的内容和格式可能有所不同。返回的字符串可能为空,但不可能为 null。
JComponent 中的 paramStringJList 的字符串表示形式。public AccessibleContext getAccessibleContext()
Accessible 中的 getAccessibleContextJComponent 中的 getAccessibleContext
|
JavaTM 2 Platform Standard Ed. 5.0 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。