treeselectionlistener.html
来自「jsf、swing的官方指南」· HTML 代码 · 共 563 行 · 第 1/2 页
HTML
563 行
<div class=PrintHeaders> <b>Trail:</b> Creating a GUI with JFC/Swing <br><b>Lesson:</b> Writing Event Listeners <br><b>Section:</b> Implementing Listeners for Commonly Handled Events </div> <div id=LeftBar class=LeftBar_shown> <div id=Contents> <div class="linkLESSON"><a href="index.html">Writing Event Listeners</a></div><div class="linkAHEAD"><a href="intro.html">Introduction to Event Listeners</a></div><div class="linkAHEAD"><a href="generalrules.html">General Information about Writing Event Listeners</a></div><div class="linkAHEAD"><a href="eventsandcomponents.html">Listeners Supported by Swing Components</a></div><div class="linkAHEAD"><a href="handling.html">Implementing Listeners for Commonly Handled Events</a></div><div class="linkBHEAD"><a href="actionlistener.html">How to Write an Action Listener</a></div><div class="linkBHEAD"><a href="caretlistener.html">How to Write a Caret Listener</a></div><div class="linkBHEAD"><a href="changelistener.html">How to Write a Change Listener</a></div><div class="linkBHEAD"><a href="componentlistener.html">How to Write a Component Listener</a></div><div class="linkBHEAD"><a href="containerlistener.html">How to Write a Container Listener</a></div><div class="linkBHEAD"><a href="documentlistener.html">How to Write a Document Listener</a></div><div class="linkBHEAD"><a href="focuslistener.html">How to Write a Focus Listener</a></div><div class="linkBHEAD"><a href="internalframelistener.html">How to Write an Internal Frame Listener</a></div><div class="linkBHEAD"><a href="itemlistener.html">How to Write an Item Listener</a></div><div class="linkBHEAD"><a href="keylistener.html">How to Write a Key Listener</a></div><div class="linkBHEAD"><a href="listdatalistener.html">How to Write a List Data Listener</a></div><div class="linkBHEAD"><a href="listselectionlistener.html">How to Write a List Selection Listener</a></div><div class="linkBHEAD"><a href="mouselistener.html">How to Write a Mouse Listener</a></div><div class="linkBHEAD"><a href="mousemotionlistener.html">How to Write a Mouse-Motion Listener</a></div><div class="linkBHEAD"><a href="mousewheellistener.html">How to Write a Mouse-Wheel Listener</a></div><div class="linkBHEAD"><a href="propertychangelistener.html">How to Write a Property Change Listener</a></div><div class="linkBHEAD"><a href="tablemodellistener.html">How to Write a Table Model Listener</a></div><div class="linkBHEAD"><a href="treeexpansionlistener.html">How to Write a Tree Expansion Listener</a></div><div class="linkBHEAD"><a href="treemodellistener.html">How to Write a Tree Model Listener</a></div><div class="nolinkBHEAD">How to Write a Tree Selection Listener</div><div class="linkBHEAD"><a href="treewillexpandlistener.html">How to Write a Tree-Will-Expand Listener</a></div><div class="linkBHEAD"><a href="undoableeditlistener.html">How to Write an Undoable Edit Listener</a></div><div class="linkBHEAD"><a href="windowlistener.html">How to Write Window Listeners</a></div><div class="linkAHEAD"><a href="api.html">Listener API Table</a></div><div class="linkAHEAD"><a href="problems.html">Solving Common Event-Handling Problems</a></div></div> </div> <div id=MainFlow class=MainFlow_indented> <span id=BreadCrumbs> <a href=../../index.html target=_top>Home Page</a> > <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a> > <a href=index.html target=_top>Writing Event Listeners</a> </span> <div class=NavBit> <a target=_top href=treemodellistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=treewillexpandlistener.html>Next »</a> </div> <div id=PageTitle>How to Write a Tree Selection Listener</div> <blockquote>To detect when the user selects a node in a<a class="TutorialLink" target="_top" href="../components/tree.html">tree</a>, you need to register a tree selection listener.Here is an example, taken from the <code>TreeDemo</code> example discussed in<a class="TutorialLink" target="_top" href="../components/tree.html#select">Responding to Node Selection</a>, of detecting node selectionin a tree that can have at most one node selected at a time:<p><font color=red>[PENDING: Update this code snippet after example has beenmodified to remove the inner class.]</font><blockquote><pre>tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (node == null) return; Object nodeInfo = node.getUserObject(); ... <em>/* React to the node selection. */</em> ... }});</pre></blockquote>To specify that the tree should support single selection,the program uses this code:<blockquote><pre>tree.getSelectionModel().setSelectionMode (TreeSelectionModel.SINGLE_TREE_SELECTION);</pre></blockquote>The <code>TreeSelectionModel</code> interfacedefines three values for the selection mode:<dl><dt> <b><code>DISCONTIGUOUS_TREE_SELECTION</code></b><dd> This is the default mode for the default tree selection model. With this mode, any combination of nodes can be selected.<dt> <b><code>SINGLE_TREE_SELECTION</code></b><dd> This is the mode used by the preceding example. At most one node can be selected at a time.<dt> <b><code>CONTIGUOUS_TREE_SELECTION</code></b><dd> With this mode, only nodes in adjoining rows can be selected.</dl></blockquote><h3><a name="api">The Tree Selection Listener API</a></h3><blockquote><p align=center><a name="treeselectionlistener">The TreeSelectionListener Interface</a><p><em>Because <code>TreeSelectionListener</code> has only one method, it has no corresponding adapter class.</em><table border=1><tr><th align=left>Method</th><th align=left>Purpose</th></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeSelectionListener.html#valueChanged(javax.swing.event.TreeSelectionEvent)">valueChanged(TreeSelectionEvent)</a></td><td> Called whenever the selection changes.</td></tr></table><p align=center><a name="treeexpansionevent">The TreeExpansionEvent API</a><p><table border=1><tr><th align=left>Method</th><th align=left>Purpose</th></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/util/EventObject.html#getSource()">Object getSource()</a><br>(<em>in <code>java.util.EventObject</code></em>)</td><td>Return the object that fired the event.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeSelectionEvent.html#getNewLeadSelectionPath()">TreePath getNewLeadSelectionPath()</a></td><td> Return the current lead path.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeSelectionEvent.html#getOldLeadSelectionPath()">TreePath getOldLeadSelectionPath()</a></td><td> Return the path that was previously the lead path.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeSelectionEvent.html#getPath()">TreePath getPath()</a></td><td> Return the first path element.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeSelectionEvent.html#getPaths()">TreePath[] getPaths()</a></td><td> Return the paths that have been added or removed from the selection.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeSelectionEvent.html#isAddedPath()">boolean isAddedPath()</a></td><td> Return true if the first path element has been added to the selection. Returns false if the first path has been removed from the selection.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeSelectionEvent.html#isAddedPath(int)">boolean isAddedPath(int)</a></td><td> Return true if the path specified by the index was added to the selection.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeSelectionEvent.html#isAddedPath(javax.swing.tree.TreePath)">boolean isAddedPath(TreePath)</a></td><td> Return true if the specified path was added to the selection.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html#getLastSelectedPathComponent()">Object getLastSelectedPathComponent()</a></td><td> Return the last path component in the first node of the current selection.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html#getLeadSelectionPath()">TreePath getLeadSelectionPath()</a><br>(<em>in <code>JTree</code></em>)</td><td> Return the current lead path.</td></tr></table></blockquote><a name="eg"><h3>Examples that Use Tree Selection Listeners</h3></a><blockquote>The following table lists theexamples that use tree selection listeners.<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="../components/examples/index.html#TreeDemo"><code>TreeDemo</code></a><br><font color=red>[PENDING: When this example has been updatedto 1.4, this link will point to it.]</font></td><td><a class="TutorialLink" target="_top" href="../components/tree.html">How to Use Trees</a></td><td> The tree listener responds to node clicks by showing the appropriate HTML document.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=treemodellistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=treewillexpandlistener.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 Write a Tree Model Listener <br><b>Next page:</b> How to Write a Tree-Will-Expand Listener </div> </body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?