|
|
< Day Day Up > |
|
How to Write a Tree-Will-Expand ListenerAs explained in How to Write a Tree Expansion Listener (page 710), you can use a tree-will-expand listener to prevent a tree node from expanding or collapsing. To be notified just after an expansion or collapse occurs, you should use a tree expansion listener instead. TreeExpandEventDemo2, shown in Figure 14, adds a tree-will-expand listener to the TreeExpandEventDemo example. The new code demonstrates the ability of tree-will-expand listeners to veto node expansions and collapses: It asks for confirmation each time you try to expand a node. Figure 14. The TreeExpandEventDemo2 application.
Try This:
The following snippet shows the code that this program adds to TreeExpandEventDemo. The bold line prevents the tree expansion from happening. You can find all the demo's source code in TreeExpandEventDemo2.java.
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);
//...show a dialog...
if (/* user said to cancel the expansion */) {
//Cancel expansion.
saySomething("Tree expansion cancelled", e);
throw new ExpandVetoException(e);
}
}
//Required by TreeWillExpandListener interface.
public void treeWillCollapse(TreeExpansionEvent e) {
saySomething("Tree-will-collapse event detected", e);
}
...
}
}
The Tree-Will-Expand Listener APITable 41 lists the methods in the TreeWillExpandListener interface. Note that this interface has no adapter class. Also refer to the TreeWillExpandListener API documentation online at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/TreeWillExpandListener.html.
See Table 36, "The TreeExpansionEvent API," on page 712 for information about the TreeExpansionEvent argument for the preceding methods. Examples That Use Tree-Will-Expand ListenersThe following examples use tree-will-expand listeners.
|
|
|
< Day Day Up > |
|