📄 jlist.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc on Thu Apr 27 23:38:52 PDT 2000 --><TITLE>Java 2 Platform SE v1.3: Class JList</TITLE><LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style"></HEAD><BODY BGCOLOR="white"><!-- ========== START OF NAVBAR ========== --><A NAME="navbar_top"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0"><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3"> <TR ALIGN="center" VALIGN="top"> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD> <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/JList.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD> </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup> 2 Platform<br>Std. Ed. v1.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../javax/swing/JLayeredPane.AccessibleJLayeredPane.html"><B>PREV CLASS</B></A> <A HREF="../../javax/swing/JList.AccessibleJList.html"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../index.html" TARGET="_top"><B>FRAMES</B></A> <A HREF="JList.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> SUMMARY: <A HREF="#inner_class_summary">INNER</A> | <A HREF="#fields_inherited_from_class_javax.swing.JComponent">FIELD</A> | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL: FIELD | <A HREF="#constructor_detail">CONSTR</A> | <A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><!-- =========== END OF NAVBAR =========== --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">javax.swing</FONT><BR>Class JList</H2><PRE><A HREF="../../java/lang/Object.html">java.lang.Object</A> | +--<A HREF="../../java/awt/Component.html">java.awt.Component</A> | +--<A HREF="../../java/awt/Container.html">java.awt.Container</A> | +--<A HREF="../../javax/swing/JComponent.html">javax.swing.JComponent</A> | +--<B>javax.swing.JList</B></PRE><DL><DT><B>All Implemented Interfaces:</B> <DD><A HREF="../../javax/accessibility/Accessible.html">Accessible</A>, <A HREF="../../java/awt/image/ImageObserver.html">ImageObserver</A>, <A HREF="../../java/awt/MenuContainer.html">MenuContainer</A>, <A HREF="../../javax/swing/Scrollable.html">Scrollable</A>, <A HREF="../../java/io/Serializable.html">Serializable</A></DD></DL><HR><DL><DT>public class <B>JList</B><DT>extends <A HREF="../../javax/swing/JComponent.html">JComponent</A><DT>implements <A HREF="../../javax/swing/Scrollable.html">Scrollable</A>, <A HREF="../../javax/accessibility/Accessible.html">Accessible</A></DL><P>A component that allows the user to select one or more objects from a list. A separate model, <code>ListModel</code>, represents the contents of the list. It's easy to display an array or vector of objects, using a <code>JList</code> constructor that builds a <code>ListModel</code> instance for you: <pre> // 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); </pre> <p> <code>JList</code> doesn't support scrolling directly. To create a scrolling list you make the <code>JList</code> the viewport view of a <code>JScrollPane</code>. For example: <pre> JScrollPane scrollPane = new JScrollPane(dataList); // Or in two steps: JScrollPane scrollPane = new JScrollPane(); scrollPane.getViewport().setView(dataList); </pre> <p> By default the <code>JList</code> selection model allows any combination of items to be selected at a time, using the constant <code>MULTIPLE_INTERVAL_SELECTION</code>. The selection state is actually managed by a separate delegate object, an instance of <code>ListSelectionModel</code>. However <code>JList</code> provides convenient properties for managing the selection. <pre> String[] data = {"one", "two", "three", "four"}; JList dataList = new JList(data); dataList.setSelectedIndex(1); // select "two" dataList.getSelectedValue(); // returns "two" </pre> <p> The contents of a <code>JList</code> can be dynamic, in other words, the list elements can change value and the size of the list can change after the <code>JList</code> has been created. The <code>JList</code> observes changes in its model with a <code>swing.event.ListDataListener</code> implementation. A correct implementation of <code>ListModel</code> notifies it's listeners each time a change occurs. The changes are characterized by a <code>swing.event.ListDataEvent</code>, which identifies the range of list indices that have been modified, added, or removed. Simple dynamic-content <code>JList</code> applications can use the <code>DefaultListModel</code> class to store list elements. This class implements the <code>ListModel</code> interface and provides the <code>java.util.Vector</code> API as well. Applications that need to provide custom <code>ListModel</code> implementations can subclass <code>AbstractListModel</code>, which provides basic <code>ListDataListener</code> support. For example: <pre> // This list model has about 2^16 elements. Enjoy scrolling. <a name="prototype_example"> 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"); </pre> <p> <code>JList</code> uses a <code>java.awt.Component</code>, provided by a delegate called the <code>cellRendererer</code>, to paint the visible cells in the list. The cell renderer component is used like a "rubber stamp" to paint each visible row. Each time the <code>JList</code> needs to paint a cell it asks the cell renderer for the component, moves it into place using <code>setBounds()</code> and then draws it by calling its paint method. The default cell renderer uses a <code>JLabel</code> component to render the string value of each component. You can substitute your own cell renderer, using code like this: <pre> // Display an icon and a string for each object in the list. <a name="cellrenderer_example"> 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -