📄 treeexpansionlistener.html
字号:
Tutorials</a> <br> <a href="javascript:toggleLeft()" id="ToggleLeft">Hide the TOC</a> </div> </div> </div> </div> </div> </div> <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="nolinkBHEAD">How to Write a Tree Expansion Listener</div><div class="linkBHEAD"><a href="treemodellistener.html">How to Write a Tree Model Listener</a></div><div class="linkBHEAD"><a href="treeselectionlistener.html">How to Write a Tree Selection Listener</a></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=tablemodellistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=treemodellistener.html>Next »</a> </div> <div id=PageTitle>How to Write a Tree Expansion Listener</div> <blockquote>Sometimes when using a <a class="TutorialLink" target="_top" href="../components/tree.html">tree</a>, you might need to react when a branch becomes expanded or collapsed.For example, you might need to load or save data.Or you might need to prevent the userfrom expanding a particular node.<p>Two kinds of listeners report expansion and collapse occurrences:tree expansion listeners and tree-will-expand listeners.This page discusses the former.A tree expansion listener detects when an expansion or collapsehas happened.In general, you should implement a tree expansion listenerunless you might need to prevent an expansion or collapsefrom happening.<p>Tree-will-expand listeners are discussed in<a href="treewillexpandlistener.html">How to Write a Tree-Will-ExpandListener</a>.A tree-will-expand listener detects when an expansion or collapseis about to happen.<p>The following example demonstrates a simple tree expansion listener.The text area at the bottom of the windowdisplays a message every time a tree expansion event occurs.It's a straightforward, simple demo.To see a more interesting version that can veto expansions, see<a href="treewillexpandlistener.html">How to Write a Tree-Will-ExpandListener</a>.<p><p><center><IMG SRC="../../figures/uiswing/events/TreeExpandEventDemo.png" WIDTH="412" HEIGHT="195" ALIGN="BOTTOM" ALT=""></center></p><blockquote><hr><strong>Try this:</strong> <ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/events/examples/TreeExpandEventDemo.jnlp">Run TreeExpandEventDemo</a> using <a href=http://java.sun.com/products/javawebstart> Java<sup><font size=-2>TM</font></sup> Web Start</a>. Or, to compile and run the example yourself, consult the <a href="examples/index.html#TreeExpandEventDemo">example index</a>.<li> Expand a node. A tree-expanded event is fired.<li> Collapse the node. A tree-collapsed event is fired.</ol><hr></blockquote><p>The following code shows how the program handles expansion events.You can find all the example's source code in<a class="SourceLink" target="_blank" href="examples/TreeExpandEventDemo.java"><code>TreeExpandEventDemo.java</code></a>.<blockquote><pre>public class TreeExpandEventDemo ... { ... void saySomething(String eventDescription, TreeExpansionEvent e) { textArea.append(eventDescription + "; " + "path = " + e.getPath() + newline); } class DemoArea ... implements TreeExpansionListener { ... public DemoArea() { ... tree.addTreeExpansionListener(this); ... } ... // Required by TreeExpansionListener interface. public void treeExpanded(TreeExpansionEvent e) { saySomething("Tree-expanded event detected", e); } // Required by TreeExpansionListener interface. public void treeCollapsed(TreeExpansionEvent e) { saySomething("Tree-collapsed event detected", e); } }}</pre></blockquote></blockquote><h3><a name="api">The Tree Expansion Listener API</a></h3><blockquote><p align=center><a name="treeexpansionlistener">The TreeExpansionListener Interface</a><p><em><code>TreeExpansionListener</code> has no 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/TreeExpansionListener.html#treeCollapsed(javax.swing.event.TreeExpansionEvent)">treeCollapsed(TreeExpansionEvent)</a></td><td> Called just after a tree node collapses.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeExpansionListener.html#treeExpanded(javax.swing.event.TreeExpansionEvent)">treeExpanded(TreeExpansionEvent)</a></td><td> Called just after a tree node expands.</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></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/TreeExpansionEvent.html#getPath()">TreePath getPath()</a></td><td>Returns a <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreePath.html"><code>TreePath</code></a> object that identifies each node from the root of the tree to the collapsed/expanded node, inclusive.</td></tr></table></blockquote><a name="eg"><h3>Examples that Use Tree Expansion Listeners</h3></a><blockquote>The following table lists theexamples that use tree expansion 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="examples/index.html#TreeExpandEventDemo"> <code>TreeExpandEventDemo</code></a></td><td> This section</td><td> Displays a message whenever a tree expansion event occurs.</td></tr><tr valign=top><td> <a href="examples/index.html#TreeExpandEventDemo2"> <code>TreeExpandEventDemo2</code></a></td><td> <a href="treewillexpandlistener.html">How to Write a Tree-Will-Expand Listener</a></td><td> Adds a tree-will-expand listener to <code>TreeExpandEventDemo</code>.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=tablemodellistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=treemodellistener.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 Table Model Listener <br><b>Next page:</b> How to Write a Tree Model Listener </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -