窃取另一个应用程序树视图的内容

发布于 2024-08-26 07:30:03 字数 377 浏览 7 评论 0 原文

我有一个用 Java 编写的带有非常大的 TreeView 控件的应用程序。我想仅在叶子的类似 XPath 元素的列表(只是字符串而不是 JList)中获取树控件的内容。这是一个例子 root

|-Item1
  |-Item1.1
    |-Item1.1.1 (leaf)
  |-Item1.2 (leaf)
|-Item2
  |-Item2.1 (leaf)

会输出:

/Item1/Item1.1/Item1.1.1
/Item1/Item1.2
/Item2/Item2.1

我没有任何源代码或类似的东西。是否有我可以使用的工具来深入研究 Window 项目本身并提取这些数据?我不介意是否有一些后处理步骤,因为手动输入是我唯一的其他选择。

I have an application with a very large TreeView control in Java. I want to get the contents of the tree control in a list (just strings not a JList) of XPath-like elements of leaves only. Here's an example
root

|-Item1
  |-Item1.1
    |-Item1.1.1 (leaf)
  |-Item1.2 (leaf)
|-Item2
  |-Item2.1 (leaf)

Would output:

/Item1/Item1.1/Item1.1.1
/Item1/Item1.2
/Item2/Item2.1

I don't have any source code or anything handy like that. Is there I tool I can use to dig into the Window item itself and pull out this data? I don't mind if there are a few post-processing steps because typing it in by hand is my only other option.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的笑 2024-09-02 07:30:03

如果我们假设您有一个 TreeModel (您可以使用 JTree.getModel()JTree 获取它),那么以下代码将以您要查找的“/”分隔格式打印出树的叶子:

/**
 * 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);
        }
    }
}

当然,如果您不仅仅想将树的内容打印到控制台,您可以替换 println 语句与其他内容,例如输出到文件,或者写入或附加到作为附加参数传递给这些方法的 WriterStringBuilder

If we assume that you have a TreeModel (which you can get from a JTree using JTree.getModel()), then the following code would print out the leaves of the tree in the "/"-separated format that you are looking for:

/**
 * 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);
        }
    }
}

Of course, if you didn't just want to print the tree's contents to the console, you could replace the println statements with something else, such as output to a file or such as writing or appending to a Writer or a StringBuilder that is passed to these methods as an additional argument.

贩梦商人 2024-09-02 07:30:03

(我正在发布第二个答案,具体取决于问题的解释...)

如果您已经知道在拥有 JTree 后该做什么,并且您只是想找到 Container 中的 >JTree 组件(包括任何 JComponentWindowJFrame 等) ),那么以下代码将搜索给定的Container并返回它找到的第一个JTree(如果没有JTree则返回null)代码>可以找到):

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

(I'm posting a second answer, depending on the interpretation of the question...)

If you already know what to do once you have a JTree and you're just trying to find the JTree component in an arbitrary Container (including any JComponent, Window, JFrame, etc.), then the following code will search the given Container and return the first JTree it finds (or null if no JTree can be found):

/**
 * 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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文