相同的数据集,两个不同的 JTable
我有一些数据必须通过两个 JTables 显示;数据是相同的,但每个表的显示方式必须略有不同。 另外,我从外部连接接收数据(在本例中为 JMS,但这并不重要,它可能是数据库,或其他)。
由于我是 Swing 新手,我仍然有点困惑谁应该触发事件、谁应该监听事件以及如何修改我的数据集,以便我可以更新两个表。
现在,我的数据集结构以及一些虚拟数据的一个小示例:
class Student{ String name; Classroom classroom; boolean goodStudent}
class Classroom{ Sting name; List<String> coursesTaught; List<Student> students;}
public List<Classroom> classes;
基本上,我的数据集将是 Controller 类中的 classes
字段,并且两个 JTable 必须以不同的方式显示内容。 也就是说,表 1 必须显示如下内容:
Classroom Name | Courses
4a | CS101, CS102, CS103
4b | BM101, CS102
4c | I101, CS4100
基本上,对于每个教室,课程列表。
表 2 应该显示如下内容:
Student Name | Good?
Mark Spencer | true
Philippe Mann | true
Tom Sayer | false
我应该看到所有教室的所有学生。
正如您所看到的,数据是相同的,但显示方式不同。 我想做的是,当我的数据发生变化时,表也会自动更新。 就我目前的理解而言,我必须对 AbstractTableModel 进行子类化,并为我想要显示的数据类型创建两个不同的 TableModel;我不明白的是:
- 一旦发生一些变化,模型将如何获取数据?
- 谁应该将这一变化通知模型?
- 调用“fireTableXXXEvent()”是否足以触发视图刷新?
我希望我已经说得足够清楚了...... 无论如何,非常感谢! 再见
I have some data I have to show through two JTables; the data is the same, but each table will have to show it a little differently.
Also, I receive the data from an external connection (JMS in this case, but it doesn't really matter, it could be a DB, or whatever).
Being that I am new to Swing, I am still a little confused about who should fire the events, who should listen to them and how to make so that for a modification to my dataset I will have both the tables to update.
Now, a little example of my dataset structure as well as some dummy data:
class Student{ String name; Classroom classroom; boolean goodStudent}
class Classroom{ Sting name; List<String> coursesTaught; List<Student> students;}
public List<Classroom> classes;
Basically, my dataset will be the classes
field in a Controller class and the two JTables will have to show things in different ways.
Namely, Table1 will have to show something like:
Classroom Name | Courses
4a | CS101, CS102, CS103
4b | BM101, CS102
4c | I101, CS4100
So basically, for each Classroom, the list of courses.
Table2 should instead show things like:
Student Name | Good?
Mark Spencer | true
Philippe Mann | true
Tom Sayer | false
I should see ALL the students, from all classrooms.
As you can see, the data is the same, but it is shown in different way.
What I would like to do is that, when my data changes, the tables will automatically update too.
For what I understood so far, I will have to subclass AbstractTableModel and create two different TableModels for the kind of data I want to show; what I don't get is:
- How will the Models get their data, once some change happens?
- Who should notify the models of this change?
- Is it enough to call "fireTableXXXEvent()" to trigger the view refresh?
I hope I made myself clear enough...
In any case, thank you very much!
Bye
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@StanislavL 对于每个
JTable
都需要一个TableModel
是正确的,但没有任何证据表明它们不能有效地从一个共同的、抽象的父级继承下来。在下面的(有点做作的)概述中,两个模型共享一个共同的getColumnCount()
实现,而具体的子模型则实现其余必需的TableModel
方法。@StanislavL is right about needing a
TableModel
for eachJTable
, but nothing says they can't usefully descend from a common, abstract parent. In the (somewhat contrived) outline below, the two models share a commongetColumnCount()
implementation, while the concrete children implement the remaining requiredTableModel
methods.你是对的。您需要 2 个 AbstractTableModel。
假设您有列表 ListclassesListclasses 作为主要数据源。第一个模型行计数将仅返回列表的 size() 。第二个将返回每个教室的学生人数总和。两个模型的列数都是 2。有趣的方法是 getValueAt/setValueAt,您必须找到正确的行。对于第一个列表,很简单,只需适当的列表项即可。对于第二个表模型,您必须计算正确的行,然后迭代学生列表。
假设数据库中发生了某些变化。您检索到一个新列表。您可以找到更改的内容,插入/删除/更改了哪些行/列,或者只是替换两个模型中的数据源列表并触发结构更改事件,以让 JTable 完全刷新内容。
模型不会自动更新。您可以编写例如计时器来检查每秒/分钟/小时的更改,或在重新打开显示表格的对话框/框架时刷新。
You are right. You need 2 AbstractTableModels.
Suppose you have the list List classesList classes as main data source. The first model row count will just return size() of the list. The second one will return sum of Students counts for each classroom. Column count for both models is 2. The inteeresting methods are getValueAt/setValueAt you have to find proper row. For the first list it's easy just appropriate list item. For the second table model you have to calculate proper row and then iterate through the list of Students.
Suppose something is changed in DB. You retrieve a new List. You can either find what's changed, which rows/cols were inserted/removed/changed or just replase the data source list in both models and fire structure changed event to let JTable completely refresh content.
Thre is no automatic update of models. You can write e.g. timer to check the changes each second/minute/hour or refresh on reopening the dialog/frame where the table is shown.