在开发系统的时候,尤其是前端需要展现一棵树的时候,经常需要构建一棵树来展现层级结构。
java中swing自带的tree就能满足这些需求,各种遍历方式都有。
参考资料: https://docs.oracle.com/javase/tutorial/uiswing/components/tree.html
import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.util.Arrays; import java.util.Enumeration; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeNode; /** * @ClassName: TreeExample * @author kanpiaoxue * @version 1.0 * @CreateTime: 2020/05/20 14:22:41 * @Description: */ public class TreeExample { private static final DefaultMutableTreeNode root = new DefaultMutableTreeNode(new BookInfo(1, "name-root")); private static final DefaultMutableTreeNode note01 = new DefaultMutableTreeNode(new BookInfo(2, "name-01")); private static final DefaultMutableTreeNode note02 = new DefaultMutableTreeNode(new BookInfo(3, "name-02")); private static final DefaultMutableTreeNode note03 = new DefaultMutableTreeNode(new BookInfo(4, "name-03")); private static final DefaultMutableTreeNode note04 = new DefaultMutableTreeNode(new BookInfo(5, "name-04")); private static final DefaultMutableTreeNode note05 = new DefaultMutableTreeNode(new BookInfo(6, "name-05")); private static final DefaultMutableTreeNode note06 = new DefaultMutableTreeNode(new BookInfo(7, "name-06")); private static final DefaultMutableTreeNode note07 = new DefaultMutableTreeNode(new BookInfo(8, "name-07")); private static final DefaultMutableTreeNode note08 = new DefaultMutableTreeNode(new BookInfo(9, "name-08")); private static final DefaultMutableTreeNode note09 = new DefaultMutableTreeNode(new BookInfo(10, "name-09")); private static final DefaultMutableTreeNode note10 = new DefaultMutableTreeNode(new BookInfo(11, "name-10")); private static final Gson gson = new GsonBuilder() .registerTypeAdapter(DefaultMutableTreeNode.class, new DefaultMutableTreeNodeSerializer()) .registerTypeAdapter(DefaultMutableTreeNode.class, new DefaultMutableTreeNodeDeserializer()) .setPrettyPrinting().create(); @SuppressWarnings("unchecked") public static void main(String[] args) { DefaultMutableTreeNode root = createNodes(); Enumeration<DefaultMutableTreeNode> rootEnumeration = root.breadthFirstEnumeration(); while (rootEnumeration.hasMoreElements()) { DefaultMutableTreeNode n = rootEnumeration.nextElement(); System.out.println("-->" + n); } System.out.println("root:" + note10.getRoot()); TreeNode[] path = note10.getPath(); Arrays.stream(path).forEach(n -> { System.out.println(n); }); String json = gson.toJson(root); System.out.println(json); DefaultMutableTreeNode rootClone = gson.fromJson(json, DefaultMutableTreeNode.class); System.out.println(rootClone.getDepth()); } private static DefaultMutableTreeNode createNodes() { /** * <pre> * 树的结构: * https://www.draw.io/?lightbox=1&highlight=0000ff&edit=_blank&layers=1&nav=1&title=Untitled%20Diagram.drawio#R5ZlBb5swFMc%2FDcdK2CaEXJt1a6VNmsahZyu8AhPwkGNC0k8%2FJxgIc5exw2KnPQF%2FP4P94%2F31jPHYutx%2FEbzOvmEChUf9ZO%2BxTx6lNFoxdTgqh04hPgk6JRV5orVRiPNX6AO12uQJbCeBErGQeT0VN1hVsJETjQuB7TTsBYvpU2uegiHEG16Y6nOeyKxTI7oc9UfI06x%2FMglXXUvJ%2B2A9k23GE2zPJPbgsbVAlN1ZuV9DcaTXc%2Bn6ff5D6zAwAZWc0yF6%2Ff6SyOcYszL%2BsX6qavb4dKdfz44XjZ6wHqw89AQENlUCx5v4HrtHITNMseLFV8RaiUSJP0HKg353vJGopEyWhW7t7giJAXocuZa22IgNXBhunwFcpCAvxNGBr8pMwBKkOKh%2BAgou8910HFxnSDrEjRDVieb4D0wXf2d6hBHrywordbi%2FRcyBTczLD4M5tImZrD4MZxJZBW1wJgbokeoRUZvlEuKanybfqgL8FsEdCAn7ywzNKfcd%2BuKlyzf19XU71sIhJjurg4H%2FvyhFBqZTCXWNVGQb1O3bNpxpW6uuDQ3MzLlcZNR2MpqmDZ2jFAS2KZEZq3HHPRvMLbVWV%2BiBwTmwno5DWjljWkINTEvnMDngWnPhdmuupXNd67%2F9bq7jWjMdL21WXCcd74hzrn0H2w%2F%2B3Hy0ugFB3sEOxGzSdvcgfIP0wjnr269E9PYrEZn70UetliJifvatrCfkbyujRWg9H03b9p5xB1NofaeGmJ8jkXVKall7vXRSl%2BN%2FsFPb2e9E9vAL * </pre> */ root.add(note01); note01.add(note02); note01.add(note03); note01.add(note04); note02.add(note05); note05.add(note08); note05.add(note09); note09.add(note10); note03.add(note06); note04.add(note07); return root; } }
输出内容:
写道
-->BookInfo [id=1, name=name-root]
-->BookInfo [id=2, name=name-01]
-->BookInfo [id=3, name=name-02]
-->BookInfo [id=4, name=name-03]
-->BookInfo [id=5, name=name-04]
-->BookInfo [id=6, name=name-05]
-->BookInfo [id=7, name=name-06]
-->BookInfo [id=8, name=name-07]
-->BookInfo [id=9, name=name-08]
-->BookInfo [id=10, name=name-09]
-->BookInfo [id=11, name=name-10]
root:BookInfo [id=1, name=name-root]
BookInfo [id=1, name=name-root]
BookInfo [id=2, name=name-01]
BookInfo [id=3, name=name-02]
BookInfo [id=6, name=name-05]
BookInfo [id=10, name=name-09]
BookInfo [id=11, name=name-10]
{
"allowsChildren": true,
"userObject": {
"id": 1,
"name": "name-root"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 2,
"name": "name-01"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 3,
"name": "name-02"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 6,
"name": "name-05"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 9,
"name": "name-08"
}
},
{
"allowsChildren": true,
"userObject": {
"id": 10,
"name": "name-09"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 11,
"name": "name-10"
}
}
]
}
]
}
]
},
{
"allowsChildren": true,
"userObject": {
"id": 4,
"name": "name-03"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 7,
"name": "name-06"
}
}
]
},
{
"allowsChildren": true,
"userObject": {
"id": 5,
"name": "name-04"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 8,
"name": "name-07"
}
}
]
}
]
}
]
}
5
-->BookInfo [id=2, name=name-01]
-->BookInfo [id=3, name=name-02]
-->BookInfo [id=4, name=name-03]
-->BookInfo [id=5, name=name-04]
-->BookInfo [id=6, name=name-05]
-->BookInfo [id=7, name=name-06]
-->BookInfo [id=8, name=name-07]
-->BookInfo [id=9, name=name-08]
-->BookInfo [id=10, name=name-09]
-->BookInfo [id=11, name=name-10]
root:BookInfo [id=1, name=name-root]
BookInfo [id=1, name=name-root]
BookInfo [id=2, name=name-01]
BookInfo [id=3, name=name-02]
BookInfo [id=6, name=name-05]
BookInfo [id=10, name=name-09]
BookInfo [id=11, name=name-10]
{
"allowsChildren": true,
"userObject": {
"id": 1,
"name": "name-root"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 2,
"name": "name-01"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 3,
"name": "name-02"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 6,
"name": "name-05"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 9,
"name": "name-08"
}
},
{
"allowsChildren": true,
"userObject": {
"id": 10,
"name": "name-09"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 11,
"name": "name-10"
}
}
]
}
]
}
]
},
{
"allowsChildren": true,
"userObject": {
"id": 4,
"name": "name-03"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 7,
"name": "name-06"
}
}
]
},
{
"allowsChildren": true,
"userObject": {
"id": 5,
"name": "name-04"
},
"children": [
{
"allowsChildren": true,
"userObject": {
"id": 8,
"name": "name-07"
}
}
]
}
]
}
]
}
5
参考资料:
《How to serialize DefaultMutableTreeNode (Java) to JSON?》 https://stackoverflow.com/questions/53997112/how-to-serialize-defaultmutabletreenode-java-to-json
import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import java.util.Collections; import javax.swing.tree.DefaultMutableTreeNode; public class DefaultMutableTreeNodeSerializer implements JsonSerializer<DefaultMutableTreeNode> { /* (non-Javadoc) * @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type, com.google.gson.JsonSerializationContext) */ @SuppressWarnings("unchecked") @Override public JsonElement serialize(DefaultMutableTreeNode src, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("allowsChildren", src.getAllowsChildren()); jsonObject.add("userObject", context.serialize(src.getUserObject())); if (src.getChildCount() > 0) { jsonObject.add("children", context.serialize(Collections.list(src.children()))); } return jsonObject; } }
import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; import java.util.List; import javax.swing.tree.DefaultMutableTreeNode; public class DefaultMutableTreeNodeDeserializer implements JsonDeserializer<DefaultMutableTreeNode> { private static class POJO { private boolean allowsChildren; private Object userObject; private List<POJO> children; // no need for: POJO parent public DefaultMutableTreeNode toDefaultMutableTreeNode() { DefaultMutableTreeNode node = new DefaultMutableTreeNode(); node.setAllowsChildren(allowsChildren); node.setUserObject(userObject); if (children != null) { for (POJO child : children) { node.add(child.toDefaultMutableTreeNode()); // recursion! // this did also set the parent of the child-node } } return node; } // Following setters needed by Gson's deserialization: @SuppressWarnings("unused") public void setAllowsChildren(boolean allowsChildren) { this.allowsChildren = allowsChildren; } @SuppressWarnings("unused") public void setUserObject(Object userObject) { this.userObject = userObject; } @SuppressWarnings("unused") public void setChildren(List<POJO> children) { this.children = children; } } /* (non-Javadoc) * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type, com.google.gson.JsonDeserializationContext) */ @Override public DefaultMutableTreeNode deserialize(JsonElement json, java.lang.reflect.Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return context.<POJO>deserialize(json, POJO.class).toDefaultMutableTreeNode(); } }
public class BookInfo { private Integer id; private String name; /** * @param id * @param name */ public BookInfo(Integer id, String name) { super(); this.id = id; this.name = name; } /* * (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } BookInfo other = (BookInfo) obj; if (name == null) { if (other.name != null) { return false; } } else if (!name.equals(other.name)) { return false; } return true; } /** * @return the id */ public Integer getId() { return id; } /** * @return the name */ public String getName() { return name; } /* * (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } /** * @param id * the id to set */ public void setId(Integer id) { this.id = id; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } /* * (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "BookInfo [id=" + id + ", name=" + name + "]"; } }
相关推荐
本文将深入探讨几种开源的Java Tree组件,它们可以帮助开发者高效地处理和展示树状数据。 1. **JTree(Java Swing组件)** JTree是Java Swing库中的一个组件,用于在图形用户界面(GUI)中展示树形数据。它允许...
在Java编程语言中,"Tree"通常指的是数据结构中的树形结构,这是一个非线性的数据组织方式,模拟了自然界中的树状层次关系。在Java中,我们可以使用多种方式来实现树,包括但不限于自定义类、Java集合框架中的`...
在Java Web开发中,Bean类通常用于封装业务逻辑和数据,方便在不同组件之间传递信息。`DbConnector.java`可能是数据库连接相关的类,负责建立和管理与数据库的连接,这是大多数Web应用处理数据的基本步骤。 最后,`...
在Java编程语言中,"Tree"是一个重要的数据结构,它以树形结构组织数据,具有节点(Node)的概念,每个节点可以有零个或多个子节点。这种数据结构非常适合表示层次关系,例如文件系统、组织架构或者计算机科学中的...
下面是如何在 Java Swing 应用程序中使用这些组件的示例: ```java import javax.swing.*; import javax.swing.tree.DefaultTreeModel; import java.awt.*; public class CheckBoxTreeExample extends JFrame { ...
在Java编程语言中,"树"(Tree)是一种非线性的数据结构,它模拟了自然界中的树状层次关系。在计算机科学中,树被广泛应用于各种算法和数据存储,如文件系统、数据库索引、图形表示等。Java提供了多种方式来实现和...
在"java tree在web上的实现"这个场景中,jTree将用于在用户浏览器中呈现由Java后端生成的树形结构。 要将jTree整合到Web应用中,你需要在HTML页面中引入jTree的CSS和JavaScript文件,并配置所需的参数。例如,你...
在Java编程语言中,`JTree` 是Swing库中的一个关键组件,用于展示层次结构数据,例如文件系统目录或组织结构。`JTree` 提供了一种可视化的树状结构,用户可以通过它来浏览和操作数据。在这个“java-tree1.rar_jtree_...
本项目“基于JAVA的Tree树形菜单管理系统源代码”提供了一个实现这种功能的实例,展示了如何利用Java语言来创建一个美观且易用的树形菜单系统。 在Java中,树形控件的实现主要依赖于Java Swing库,特别是`javax....
标题中的"oa.rar_ext_ext java oa_ext oa_ext tree_java ext tree"似乎是一个组合字符串,它可能代表了某个项目或教程的文件结构。其中,“oa”可能是项目或系统的名称,而“ext”、“java”、“oa_ext”、“tree_...
理解并掌握Web Dynpro Java中的Tree组件是提升企业级应用开发能力的重要一环,它可以帮助你创建用户友好且高效的界面,提供更好的用户体验。通过深入学习和实践上述资源,你可以更深入地了解如何在SAP环境中有效地...
JavaBean是一种符合特定规范的Java类,它可以作为组件在JSP中重用。在这里,JavaBean扮演了数据模型的角色,它封装了与数据库交互的逻辑,如连接数据库、执行SQL查询、获取目录树的数据等。JavaBean的实例可以被JSP...
在Swing中,`javax.swing.tree`包提供了`JTree`组件,用于在GUI中展示树形数据。`JTree`可以自定义节点渲染、监听节点事件等。对于JavaFX,`TreeView`类是构建树形结构的主要工具,支持更多的现代UI特性。 2. **JSP...
深入讨论JSF中Tree2组件使用方法。
在Java Swing中,开发者可以使用一系列组件来构建用户界面,其中`JMenu`、`JTable`和`JTree`是三个非常关键的组件,它们在构建功能丰富的交互式应用中扮演着重要角色。 1. **JMenu组件**: `JMenu`组件用于创建...
`TreeWin.java` 文件则包含了`TreeWin` 类的定义,这个类继承自`JFrame`,并实现了`TreeSelectionListener`接口,这意味着它能够监听树组件中的节点选择变化。 在`TreeWin` 类的构造函数中,首先创建了一个`...
Java组件是Java编程中不可或缺的一部分,特别是在开发图形用户界面(GUI)时。这些组件构成了用户与应用程序交互的基础。本教程将深入讲解Java组件的使用,特别是对于初学者来说,通过实例来理解各种组件的用法是...
【标题】"ext TREE"指的是在软件开发领域中,尤其是Java Web开发中,EXT JS库中的Tree组件。EXT JS是一个强大的JavaScript用户界面库,用于构建富客户端应用。Tree组件是EXT JS中的一个重要部分,它允许开发者创建可...
TreeTag组件是一个在Java服务器端应用中常用的UI组件库,主要用在JSP(JavaServer Pages)环境中。TLD(Tag Library Descriptor)是这个组件的核心,它定义了自定义标签库及其行为。在这个名为“tld.zip”的压缩包中...
在Java开发中,`javaTree`是一个强大的工具,用于构建和操作树形数据结构,尤其在GUI应用中,如文件浏览器、目录结构展示等场景下非常常见。下面,我们将深入分析一个具体的`javaTree`示例代码,理解其核心功能和...