문제

I have a large JTree displayed in a Swing panel. I've implemented some nodes which extend from DefaultMutableNode, which calculate their children dynamically, and everything is working well.

In some cases (specifically, when the expanded node has exactly one child), I'd like the JTree to expand more than one node when use clicks the expand element (recursively expanding child nodes as long as there is exactly one child, perhaps up to some limit.

For example, if I had the following tree:

A
 - B
   - B2
     - B3
       - B4a
       - B4b
 - C
 - D

and the user clicks to expand the B node, I'd like the B B2, and B3 nodes expanded right away.

도움이 되었습니까?

해결책

JTree has

public void addTreeWillExpandListener(TreeWillExpandListener tel)

So you should implement the interface

public interface TreeWillExpandListener extends EventListener {
    /**
     * Invoked whenever a node in the tree is about to be expanded.
     */
    public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException;

    /**
     * Invoked whenever a node in the tree is about to be collapsed.
     */
    public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException;
}

In the treeWillExpand check whether the TreePath of the event. If the last Node in the path has just one child expand it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top