簡體   English   中英

阻止JTree中節點擴展的節點選擇

[英]Prevent node selection on node expansion in a JTree

我創建了一個JTree ,其中一些節點是自定義的,以顯示它們是可擴展的,盡管它們還沒有任何子節點。 我已經按照這個線程來實現它。

為什么? 我想動態加載樹,所以當樹擴展時,我從服務器檢索更多信息並在樹上顯示它。

我遇到的問題是,當我展開其中一個節點時,它會被選中,這不是默認節點的默認行為(您可以在不更改樹選擇的情況下展開它們)。

如何解決此問題以防止此節點在擴展時被選中?

我沒有用下面的代碼解決這個問題。 如果沒有看到你的代碼,你必須做一些我們無法猜到的事情。 嘗試使用下面的代碼並修改它以重現您遇到的問題。 通過這樣做,你很有可能會發現你在做什么不同以及你為什么會這樣做 (另見SSCCE )。

代碼已從這里獲取

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

public class DynamicTree extends JFrame {

    public class OutlineNode extends DefaultMutableTreeNode {
        private boolean areChildrenDefined = false;
        private int outlineNum;
        private int numChildren;

        public OutlineNode(int outlineNum, int numChildren) {
            this.outlineNum = outlineNum;
            this.numChildren = numChildren;
        }

        @Override
        public boolean isLeaf() {
            return false;
        }

        @Override
        public int getChildCount() {
            if (!areChildrenDefined) {
                defineChildNodes();
            }
            return super.getChildCount();
        }

        private void defineChildNodes() {
            // You must set the flag before defining children if you
            // use "add" for the new children. Otherwise you get an infinite
            // recursive loop, since add results in a call to getChildCount.
            // However, you could use "insert" in such a case.
            areChildrenDefined = true;
            for (int i = 0; i < numChildren; i++) {
                add(new OutlineNode(i + 1, numChildren));
            }
        }

        @Override
        public String toString() {
            TreeNode parent = getParent();
            if (parent == null) {
                return String.valueOf(outlineNum);
            } else {
                return parent.toString() + "." + outlineNum;
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DynamicTree(5);
            }
        });
    }

    public DynamicTree(int n) {
        super("Creating a Dynamic JTree");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Container content = getContentPane();
        JTree tree = new JTree(new OutlineNode(1, n));
        content.add(new JScrollPane(tree), BorderLayout.CENTER);
        setSize(300, 475);
        setVisible(true);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM