public class JListextends JComponentimplements Scrollable, Accessible该组件允许用户从列表中选择一个或多个对象。单独的模型 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);
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);