1

Java で非常に大きな TreeView コントロールを持つアプリケーションがあります。ツリー コントロールの内容を、葉のみの XPath のような要素のリスト (JList ではなく文字列のみ) で取得したいと考えています。これがルートの例です

|-Item1
  |-Item1.1
    |-Item1.1.1 (リーフ)
  |-Item1.2 (リーフ)
|-Item2
  |-Item2.1 (リーフ)

出力します:

/アイテム1/アイテム1.1/アイテム1.1.1
/アイテム1/アイテム1.2
/Item2/Item2.1

ソースコードやそのような便利なものはありません。ウィンドウ項目自体を掘り下げてこのデータを引き出すために使用できるツールはありますか? 手で入力することが私の唯一の他のオプションであるため、いくつかの後処理手順があってもかまいません。

4

2 に答える 2

1

( usingTreeModelから取得できる)があると仮定すると、次のコードは、探している「/」区切りの形式でツリーの葉を出力します。JTreeJTree.getModel()

/**
 * Prints the path to each leaf in the given tree to the console as a
 * "/"-separated string.
 * 
 * @param tree
 *          the tree to print
 */
private void printTreeLeaves(TreeModel tree) {
    printTreeLeavesRecursive(tree, tree.getRoot(), new LinkedList<Object>());
}

/**
 * Prints the path to each leaf in the given subtree of the given tree to
 * the console as a "/"-separated string.
 * 
 * @param tree
 *          the tree that is being printed
 * @param node
 *          the root of the subtree to print
 * @param path
 *          the path to the given node
 */
private void printTreeLeavesRecursive(TreeModel tree,
                                      Object node,
                                      List<Object> path) {
    if (tree.getChildCount(node) == 0) {
        for (final Object pathEntry : path) {
            System.out.print("/");
            System.out.print(pathEntry);
        }
        System.out.print("/");
        System.out.println(node);
    }
    else {
        for (int i = 0; i < tree.getChildCount(node); i++) {
            final List<Object> nodePath = new LinkedList<Object>(path);
            nodePath.add(node);
            printTreeLeavesRecursive(tree,
                                     tree.getChild(node, i),
                                     nodePath);
        }
    }
}

もちろん、ツリーの内容をコンソールに出力したくない場合は、ステートメントを別のものに置き換えることができます。たとえば、ファイルへの出力や、これらのメソッドに渡されるaまたは aprintlnへの書き込みまたは追加などです。追加の引数として。WriterStringBuilder

于 2010-03-25T02:27:16.617 に答える
1

(質問の解釈に応じて、2番目の回答を投稿しています...)

を取得したら何をすべきかをすでに知っていて、任意のコンポーネント( 、、などを含む)JTreeを見つけようとしているだけの場合、次のコードは指定されたを検索し、最初に見つかったものを返します (または見つからない場合):JTreeContainerJComponentWindowJFrameContainerJTreenullJTree

/**
 * Searches the component hierarchy of the given container and returns the
 * first {@link javax.swing.JTree} that it finds.
 * 
 * @param toSearch
 *          the container to search
 * @return the first tree found under the given container, or <code>null</code>
 *         if no {@link javax.swing.JTree} could be found
 */
private JTree findTreeInContainer(Container toSearch) {
    if (toSearch instanceof JTree) {
        return (JTree)toSearch;
    }
    else {
        for (final Component child : toSearch.getComponents()) {
            if (child instanceof Container) {
                JTree result = findTreeInContainer((Container)child);
                if (result != null) {
                    return result;
                }
            }
        }
        return null;
    }
}
于 2010-03-25T02:40:07.517 に答える