Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 窃取另一个应用程序的内容';s树视图_Java_Swing_Treeview_Windows Messages - Fatal编程技术网

Java 窃取另一个应用程序的内容';s树视图

Java 窃取另一个应用程序的内容';s树视图,java,swing,treeview,windows-messages,Java,Swing,Treeview,Windows Messages,我有一个应用程序,在Java中有一个非常大的TreeView控件。我只想在一个类似XPath的叶子元素列表(只是字符串而不是JList)中获取树控件的内容。这里有一个例子 根 |-项目1 |-项目1.1 |-第1.1.1项(叶) |-项目1.2(叶) |-项目2 |-项目2.1(叶) 将输出: /Item1/Item1.1/Item1.1.1 /Item1/Item1.2 /Item2/Item2.1 /第1项/第1.1项/第1.1.1项 /第1项/第1.2项 /第2项/第2.1项 我没有任何源

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

|-项目1 |-项目1.1 |-第1.1.1项(叶) |-项目1.2(叶) |-项目2 |-项目2.1(叶) 将输出:

/Item1/Item1.1/Item1.1.1 /Item1/Item1.2 /Item2/Item2.1 /第1项/第1.1项/第1.1.1项 /第1项/第1.2项 /第2项/第2.1项
我没有任何源代码或类似的东西。是否有我可以使用的工具来挖掘窗口项本身并提取这些数据?我不介意是否有一些后期处理步骤,因为手工输入是我唯一的选择。

如果我们假设您有一个
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);
        }
    }
}
/**
*将给定树中每个叶的路径打印到控制台,作为
*“/”-分隔字符串。
* 
*@param树
*要打印的树
*/
私有void打印树视图(树模型树){
PrintTreeLevesRecursive(树,tree.getRoot(),新链接列表());
}
/**
*将给定树的给定子树中的每个叶的路径打印到
*控制台显示为“/”分隔字符串。
* 
*@param树
*正在打印的树
*@param节点
*要打印的子树的根
*@param路径
*到给定节点的路径
*/
私有void printTreeLevesRecursive(树模型树,
对象节点,
列表路径){
if(tree.getChildCount(node)==0){
用于(最终对象路径条目:路径){
系统输出打印(“/”);
系统输出打印(路径输入);
}
系统输出打印(“/”);
System.out.println(节点);
}
否则{
for(int i=0;i
当然,如果您不只是想将树的内容打印到控制台,您可以使用其他内容替换
println
语句,例如输出到文件,或者写入或附加到作为附加参数传递给这些方法的
Writer
StringBuilder

(我发布了第二个答案,取决于对问题的解释…)

如果您已经知道在拥有
JTree
之后要做什么,并且您只是试图在任意
容器中查找
JTree
组件(包括任何
JComponent
窗口
JFrame
,等等),然后下面的代码将搜索给定的
容器
,并返回它找到的第一个
JTree
(如果找不到
JTree
,则返回
null
):


你是说你有一个JTree吗?我对这个问题提出了两种可能的解释,并试图回答这两种解释。希望至少有一个答案会有帮助。:-)我假设应用程序有一个JTree,但我没有直接访问它的权限。这可能需要Windows消息或类似的东西。
/**
 * 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);
        }
    }
}
/**
 * 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;
    }
}