Java 用户展开节点时展开多个JTree节点

Java 用户展开节点时展开多个JTree节点,java,swing,jtree,Java,Swing,Jtree,我有一个大的JTree显示在一个Swing面板中。我已经实现了一些从DefaultMutableNode扩展而来的节点,这些节点动态地计算它们的子节点,并且一切正常 在某些情况下(特别是,当扩展的节点只有一个子节点时),我希望当use单击expand元素时JTree可以扩展多个节点(递归地扩展子节点,只要只有一个子节点,可能会达到一定的限制) 例如,如果我有以下树: A - B - B2 - B3 - B4a - B4b - C - D 用户单

我有一个大的JTree显示在一个Swing面板中。我已经实现了一些从DefaultMutableNode扩展而来的节点,这些节点动态地计算它们的子节点,并且一切正常

在某些情况下(特别是,当扩展的节点只有一个子节点时),我希望当use单击expand元素时JTree可以扩展多个节点(递归地扩展子节点,只要只有一个子节点,可能会达到一定的限制)

例如,如果我有以下树:

A
 - B
   - B2
     - B3
       - B4a
       - B4b
 - C
 - D
用户单击展开B节点,我希望B B2和B3节点立即展开。

JTree

public void addTreeWillExpandListener(TreeWillExpandListener tel)
所以你应该实现这个接口

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;
}
treeWillExpand
中,检查事件的
TreePath
。如果路径中的最后一个
节点只有一个子节点,则将其展开。

JTree
具有

public void addTreeWillExpandListener(TreeWillExpandListener tel)
所以你应该实现这个接口

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;
}
treeWillExpand
中,检查事件的
TreePath
。如果路径中的最后一个
节点只有一个子节点,则将其展开。

JTree
具有

public void addTreeWillExpandListener(TreeWillExpandListener tel)
所以你应该实现这个接口

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;
}
treeWillExpand
中,检查事件的
TreePath
。如果路径中的最后一个
节点只有一个子节点,则将其展开。

JTree
具有

public void addTreeWillExpandListener(TreeWillExpandListener tel)
所以你应该实现这个接口

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;
}

treeWillExpand
中,检查事件的
TreePath
是否。如果路径中的最后一个
节点只有一个子节点,则展开它。

如何从侦听器展开它?我所能做的就是“否决”展开?JTree具有public void expandPath(TreePath path)方法。在TreeWireExpand中,找出要展开的TreePath,并使用SwingUtilities.invokeLater()将expandPath包装为cll。但如何从侦听器展开它?我所能做的就是“否决”展开?JTree具有公共的void expandPath(TreePath)方法。在TreeWireExpand中,找出要展开的TreePath,并使用SwingUtilities.invokeLater()将expandPath包装为cll。但如何从侦听器展开它?我所能做的就是“否决”展开?JTree具有公共的void expandPath(TreePath)方法。在TreeWireExpand中,找出要展开的TreePath,并使用SwingUtilities.invokeLater()将expandPath包装为cll。但如何从侦听器展开它?我所能做的就是“否决”展开?JTree具有公共的void expandPath(TreePath)方法。在TreeWireExpand中,找出要展开的TreePath,并在SwingUtilities.invokeLater()中cll expandPath包装