📄 treewillexpandlistener.html
字号:
function load() { showLeft(leftBar()); document.getElementById("ToggleLeft").style.display="inline"; } </script> </head><body onload="load()"> <div id=TopBar> <div id=TopBar_tr> <div id=TopBar_tl> <div id=TopBar_br> <div id=TopBar_bl> <div id=TopBar_right> <a target="_blank" href="http://java.sun.com/javase/6/download.jsp">Download the JDK</a> <br> <a href="../../search.html" target="_blank">Search the 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="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="linkBHEAD"><a href="treeselectionlistener.html">How to Write a Tree Selection Listener</a></div><div class="nolinkBHEAD">How to Write a Tree-Will-Expand Listener</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=treeselectionlistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=undoableeditlistener.html>Next »</a> </div> <div id=PageTitle>How to Write a Tree-Will-Expand Listener</div> <blockquote>As explained in <a href="treeexpansionlistener.html">How to Write a Tree Expansion Listener</a>,you can use a tree-will-expand listenerto prevent a <a class="TutorialLink" target="_top" href="../components/tree.html">tree</a> node from expanding or collapsing.To be notified just <em>after</em> an expansion or collapse occurs,you should use atree expansion listener instead.<p>The following demo adds a tree-will-expand listenerto the <code>TreeExpandEventDemo</code> examplediscussed in <a href="treeexpansionlistener.html">How toWrite a Tree Expansion Listener</a>. The new codedemonstrates the ability of tree-will-expand listenersto veto node expansions and collapses: it asks forconfirmation each time you try to expand a node.<p><p><center><IMG SRC="../../figures/uiswing/events/TreeExpandEventDemo2.png" WIDTH="414" HEIGHT="207" ALIGN="BOTTOM" ALT=""></center></p><p><blockquote><hr><strong>Try this:</strong> <ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/events/examples/TreeExpandEventDemo2.jnlp">Run TreeExpandEventDemo2</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#TreeExpandEventDemo2">example index</a>.<li> Click the graphic to the left of the <b>Potrero Hill</b> node. This tells the tree that you want to expand the node. <br> A dialog appears asking you whether you really want to expand the node.<li> Click "Expand" or dismiss the dialog. <br> Messages in the text area tell you that both a tree-will-expand event and a tree-expanded event have occurred. At the end of each message is the path to the expanded node.<li> Try to expand another node, but this time press the "Cancel Expansion" button in the dialog. <br> The node does not expand. Messages in the text area tell you that a tree-will-expand event occurred, and that you cancelled a tree expansion.<li> Collapse the <b>Potrero Hill</b> node. <br> The node collapses without a dialog appearing, because the event handler's <code>treeWillCollapse</code> method lets the collapse occur, uncontested.</ol><hr></blockquote><p>The following snippet shows the codethat this program adds to <code>TreeExpandEventDemo</code>.The bold line prevents the tree expansion from happening.You can find all the demo's source code in<a class="SourceLink" target="_blank" href="examples/TreeExpandEventDemo2.java"><code>TreeExpandEventDemo2.java</code></a>.<blockquote><pre>public class TreeExpandEventDemo2 ... { ... class DemoArea ... implements ... TreeWillExpandListener { ... public DemoArea() { ... tree.addTreeWillExpandListener(this); ... } ... //Required by TreeWillExpandListener interface. public void treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException { saySomething("Tree-will-expand event detected", e); <em>//...show a dialog...</em> if (<em>/* user said to cancel the expansion */</em>) { //Cancel expansion. saySomething("Tree expansion cancelled", e); <b>throw new ExpandVetoException(e);</b> } } //Required by TreeWillExpandListener interface. public void treeWillCollapse(TreeExpansionEvent e) { saySomething("Tree-will-collapse event detected", e); } ... }}</pre></blockquote></blockquote><h3><a name="api">The Tree-Will-Expand Listener API</a></h3><blockquote><p align=center><a name="treewillexpandlistener">The TreeWillExpandListener Interface</a><p><em><code>TreeWillExpandListener</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/TreeWillExpandListener.html#treeWillCollapse(javax.swing.event.TreeExpansionEvent)">treeWillCollapse(TreeExpansionEvent)</a></td><td> Called just before a tree node collapses. To prevent the collapse from occurring, your implementation of this method should throw a<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/ExpandVetoException.html"><code>ExpandVetoException</code></a> event.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeWillExpandListener.html#treeWillExpand(javax.swing.event.TreeExpansionEvent)">treeWillExpand(TreeExpansionEvent)</a></td><td> Called just before a tree node expands. To prevent the expansion from occurring, your implementation of this method should throw a<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/ExpandVetoException.html"><code>ExpandVetoException</code></a> event.</td></tr></table><p>See <a href="treeexpansionlistener.html#api">The Tree ExpansionEvent API</a>for information about the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeExpansionEvent.html"><code>TreeExpansionEvent</code></a> argument for the preceding methods.</blockquote><a name="eg"><h3>Examples that Use Tree-Will-Expand Listeners</h3></a><blockquote><a href="examples/index.html#TreeExpandEventDemo2"><code>TreeExpandEventDemo2</code></a>,featured in this section,is our only example that uses a tree-will-expand listener. </blockquote> <div class=NavBit> <a target=_top href=treeselectionlistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=undoableeditlistener.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 Selection Listener <br><b>Next page:</b> How to Write an Undoable Edit Listener </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -