📄 list.html
字号:
<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ListCellRenderer.html"><code>ListCellRenderer</code></a> interface.<li> Create an instance of your class and call the list's <code>setCellRenderer</code> using the instance as an argument.</ul>We don't provide an example of a listwith a custom cell renderer,but we do have an example of a combo box with a custom renderer —and combo boxes use the same type of renderer as lists.See the example described in<a href="combobox.html#renderer">Providing a Custom Renderer</a>.</blockquote><h3><a name="api">The List API</a></h3><blockquote>The following tables list the commonly used<code>JList</code> constructors and methods.Other methods you are most likely to invoke ona <code>JList</code> object are thosesuch as <code>setPreferredSize</code>that its superclasses provide.See<a href="jcomponent.html#api">The JComponent API</a>for tables of commonly used inherited methods.<p>Much of the operation of a list is managed by other objects.The items in the list are managed by a list model object,the selection is managed by a list selection model object,and most programs put a list in a scroll pane to handle scrolling.For the most part, you don't need to worry about the modelsbecause <code>JList</code> creates them as necessary andyou interact with them implicitly with <code>JList</code>'s convenience methods.<P>That said, the API for using lists falls into these categories:<ul><li><a href="#itemsapi">Initializing List Data</a><li><a href="#scrollingapi">Displaying the List</a><li><a href="#selectionapi">Managing the List's Selection</a><li><a href="#dataapi">Managing List Data</a></ul><p><table border=1><caption><a name="itemsapi">Initializing List Data</a></caption><tr><th>Method or Constructor</th><th>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#JList(javax.swing.ListModel)">JList(ListModel)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#JList(java.lang.Object[])">JList(Object[])</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#JList(java.util.Vector)">JList(Vector)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#JList()">JList()</a> </td> <td>Create a list with the initial list items specified. The second and third constructors implicitly create an immutable <code>ListModel</code>; you should not subsequently modify the passed-in array or <code>Vector</code>. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#setModel(javax.swing.ListModel)">void setModel(ListModel)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getModel()">ListModel getModel()</a> </td> <td>Set or get the model that contains the contents of the list. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#setListData(java.lang.Object[])">void setListData(Object[])</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#setListData(java.util.Vector)">void setListData(Vector)</a> </td> <td>Set the items in the list. These methods implicitly create an immutable <code>ListModel</code>. </td> </tr></table><p><table border=1><caption><a name="scrollingapi">Displaying the List</a></caption><tr><th>Method</th><th>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#setVisibleRowCount(int)">void setVisibleRowCount(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getVisibleRowCount()">int getVisibleRowCount()</a> </td> <td>Set or get how many rows of the list should be visible. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#setLayoutOrientation(int)">void setLayoutOrientation(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getLayoutOrientation()">int getLayoutOrientation()</a> </td> <td>Set or get the way list cells are laid out. The possible layout formats are specified by the <code>JList</code>-defined values <code>VERTICAL</code> (a single column of cells; the default), <code>HORIZONTAL_WRAP</code> ("newspaper" style with the content flowing horizontally then vertically), and <code>VERTICAL_WRAP</code> ("newspaper" style with the content flowing vertically then horizontally). </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getFirstVisibleIndex()">int getFirstVisibleIndex()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getLastVisibleIndex()">int getLastVisibleIndex()</a> </td> <td>Get the index of the first or last visible item. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#ensureIndexIsVisible(int)">void ensureIndexIsVisible(int)</a> </td> <td>Scroll so that the specified index is visible within the viewport that this list is in. </td> </tr></table><p><table border=1><caption><a name="selectionapi">Managing the List's Selection</a></caption><tr><th>Method</th><th>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#addListSelectionListener(javax.swing.event.ListSelectionListener)">void addListSelectionListener(ListSelectionListener)</a> </td> <td>Register to receive notification of selection changes. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#setSelectedIndex(int)">void setSelectedIndex(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#setSelectedIndices(int[])">void setSelectedIndices(int[])</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#setSelectedValue(java.lang.Object, boolean)">void setSelectedValue(Object, boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#setSelectionInterval(int, int)">void setSelectionInterval(int, int)</a> </td> <td>Set the current selection as indicated. Use <code>setSelectionMode</code> to set what ranges of selections are acceptable. The boolean argument specifies whether the list should attempt to scroll itself so that the selected item is visible. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getSelectedIndex()">int getSelectedIndex()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getMinSelectionIndex()">int getMinSelectionIndex()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getMaxSelectionIndex()">int getMaxSelectionIndex()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getSelectedIndices()">int[] getSelectedIndices()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getSelectedValue()">Object getSelectedValue()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getSelectedValues()">Object[] getSelectedValues()</a> </td> <td>Get information about the current selection as indicated. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#setSelectionMode(int)">void setSelectionMode(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getSelectionMode()">int getSelectionMode()</a> </td> <td>Set or get the selection mode. Acceptable values are: <code>SINGLE_SELECTION</code>, <code>SINGLE_INTERVAL_SELECTION</code>, or <code>MULTIPLE_INTERVAL_SELECTION</code> (the default), which are defined in <code>ListSelectionModel</code>. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#clearSelection()">void clearSelection()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#isSelectionEmpty()">boolean isSelectionEmpty()</a> </td> <td>Set or get whether any items are selected. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#isSelectedIndex(int)">boolean isSelectedIndex(int)</a> </td> <td>Determine whether the specified index is selected. </td> </tr></table><p><table border=1><caption><a name="dataapi">Managing List Data</a></caption><tr><th>Class or Method</th><th>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getNextMatch(java.lang.String, int, javax.swing.text.Position.Bias)">int getNextMatch(String, int, javax.swing.text.Position.Bias)</a> </td> <td>Given the starting index, search through the list for an item that starts with the specified string and return that index (or -1 if the string isn't found). The third argument, which specifies the search direction, can be either <code>Position.Bias.Forward</code> or <code>Position.Bias.Backward</code>. For example, if you have a 6-item list, <code>getNextMatch("Matisse", 5, javax.swing.text.Position.Bias.Forward)</code> searches for the string "Matisse" in the item at index 5, then (if necessary) at index 0, index 1, and so on. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#setDragEnabled(boolean)">void setDragEnabled(boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getDragEnabled()">boolean getDragEnabled()</a> </td> <td>Set or get the property that determines whether automatic drag handling is enabled. See<a class="TutorialLink" target="_top" href="../../dnd/index.html">Drag and Drop</a> for more details. </td> </tr></table></blockquote><h3><a name="eg">Examples that Use Lists</a></h3><blockquote>This table shows the examples that use <code>JList</code>and where those examples are described.<p><table><tr><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes</th></tr><tr valign=top><td> <a href="examples/index.html#SplitPaneDemo"><code>SplitPaneDemo</code></a></td><td> <a href="splitpane.html">How to Use Split Panes</a></td><td> Contains a single-selection, immutable list.</td></tr><tr valign=top><td> <a href="examples/index.html#ListDemo"><code>ListDemo</code></a></td><td> This section</td><td> Demonstrates how to add and remove items from a list at runtime.</td></tr><tr valign=top><td> <a href="examples/index.html#ListDialog"><code>ListDialog</code></a></td><td> This section, <a href="../layout/box.html">How to Use BoxLayout</a></td><td> Implements a modal dialog with a single-selection list.</td></tr><tr valign=top><td> <a href="../events/examples/index.html#ListDataEventDemo"><code>ListDataEventDemo</code></a></td><td> <a href="../events/listdatalistener.html"> How to Write a List Data Listener</a></td><td> Demonstrates listening for list data events on a list model.</td></tr><tr valign=top><td> <a href="../events/examples/index.html#ListSelectionDemo"><code>ListSelectionDemo</code></a></td><td> <a href="../events/listselectionlistener.html"> How to Write a List Selection Listener</a></td><td> Contains a list and a table that share the same selection model. You can dynamically choose the selection mode.</td></tr><tr valign=top><td> <a href="examples/index.html#SharedModelDemo"><code>SharedModelDemo</code></a></td><td> <a href="model.html">Using Models</a></td><td> Modifies <code>ListSelectionDemo</code> so that the list and table share the same data model.</td></tr><tr valign=top><td> <a href="examples/index.html#CustomComboBoxDemo"> <code>CustomComboBoxDemo</code></a></td><td> <a href="combobox.html#renderer">Providing a Custom Renderer</a></td><td> Shows how to provide a custom renderer for a combo box. Because lists and combo boxes use the same type of renderer, you can use what you learn there an apply it to lists. In fact, a list and a combo box can share a renderer.</td></tr></table></blockquote> </blockquote> <div class=NavBit> <a target=_top href=layeredpane.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=menu.html>Next »</a> </div> </div> <div id=Footer><div id=TagNotes> Problems with the examples? Try <a target="_blank" href=../../information/run-examples.html>Compiling and Running the Examples: FAQs</a>. <br> Complaints? Compliments? Suggestions? <a target="_blank" href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give us your feedback</a>.<br><br> <a target="_blank" href="../../information/copyright.html">Copyright</a> 1995-2006 Sun Microsystems, Inc. All rights reserved. <span id=Download></span></div> </div> <div class=PrintHeaders> <b>Previous page:</b> How to Use Layered Panes <br><b>Next page:</b> How to Use Menus </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -