我有一个用 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.
发布评论
评论(2)
如果我们假设您有一个
TreeModel
(您可以使用JTree.getModel()
从JTree
获取它),那么以下代码将以您要查找的“/”分隔格式打印出树的叶子:当然,如果您不仅仅想将树的内容打印到控制台,您可以替换
println 语句与其他内容,例如输出到文件,或者写入或附加到作为附加参数传递给这些方法的
Writer
或StringBuilder
。If we assume that you have a
TreeModel
(which you can get from aJTree
usingJTree.getModel()
), then the following code would print out the leaves of the tree in the "/"-separated format that you are looking for: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 aWriter
or aStringBuilder
that is passed to these methods as an additional argument.(我正在发布第二个答案,具体取决于问题的解释...)
如果您已经知道在拥有
JTree
后该做什么,并且您只是想找到Container 中的 >JTree 组件(包括任何
JComponent
、Window
、JFrame
等) ),那么以下代码将搜索给定的Container
并返回它找到的第一个JTree
(如果没有JTree
则返回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 theJTree
component in an arbitraryContainer
(including anyJComponent
,Window
,JFrame
, etc.), then the following code will search the givenContainer
and return the firstJTree
it finds (ornull
if noJTree
can be found):