简体   繁体   English

同步JTable和JTree

[英]Synchronizing JTable and JTree

有没有办法让JTable和JTree共享相同的模型,以便底层模型中的任何更改都会立即反映在两个组件中?

I am not sure if this is what you are looking for, but there is a two part tutorial from Sun on creating tree tables, which is essentially a JTable with a JTree renderer in the first column. 我不确定这是否是您正在寻找的,但Sun有一个关于创建树表的两部分教程,它本质上是第一列中带有JTree渲染器的JTable。 tutorial links: part 1 part 2 教程链接: 第1 部分第2部分

EDIT: 编辑:

On the topic of a TreeTable, (a Table component that maintains a tree in its first column, which allows for the hiding and displaying of rows based on the user's modifications of the Tree) I have found an implementation by NetBeans called Outline . 关于TreeTable的主题(一个在第一列中维护树的Table组件,它允许根据用户对Tree的修改隐藏和显示行)我找到了一个名为Outline的NetBeans实现。 It is very easy to use. 这是非常容易使用。 A simple example took less than 30 minutes to mock up. 一个简单的例子花了不到30分钟来模拟。 The code can be found in this answer . 代码可以在这个答案中找到。

Here is an image of the TreeTable: 这是TreeTable的图像:

alt text http://img17.imageshack.us/img17/6643/picture1hz.png 替代文字http://img17.imageshack.us/img17/6643/picture1hz.png

如果你有一个可以表示为树和表的Obj类型,你可以创建一个TableModel和一个TreeModel来观察Obj变化并做出相应的响应,你可以让Obj实现TableModelTreeModel (虽然我不是就像实现GUI对象的业务对象一样,或者你可以创建一个实现TableModelTreeModel的类,并知道何时发生对Obj更改。

It's been stated, the best way is to create a datastructure(model) of some kind to represent your data, and then have the treemodel, and tablemodel look to the common datastructure to pull the data. 已经说过,最好的方法是创建某种数据结构(模型)来表示你的数据,然后让树模型和tablemodel查看公共数据结构来提取数据。 Doing this will allow both of them to share the same model, you will just need to fire the correct events when data changes so that both of them are updated. 这样做将允许它们共享相同的模型,您只需要在数据更改时触发正确的事件,以便更新它们。

Take a look at GlazedLists -- there's an ability to use an EventList for both a JTable and a JTree. 看看GlazedLists - 可以为JTable和JTree使用EventList。 I'm not familiar with the JTree rendering, but the JTable part of GlazedLists is pretty solid. 我不熟悉JTree渲染,但GlazedLists的JTable部分非常可靠。

接口是不同的,但使用下面相同的数据结构实现它们应该是完全可行的。

假设您希望树节点包含每条记录的属性和每条记录一个表行,那么基于记录列表为TableModel和TreeModel接口创建适配器应该不会太难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM