使用JTabbedPane中的多个JTable检测onChanged()事件的表 [英] Detecting the table of an onChanged() event with multiple JTable in a JTabbedPane

查看:59
本文介绍了使用JTabbedPane中的多个JTable检测onChanged()事件的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用JFrame来保存包含多个表的JTabbedPane.在extends JFrameimplements TableModelListener的类中,我有一个onChanged()方法,该方法将TableModelEvent作为参数.我可以从触发该事件的表上成功地从该事件中获取数据,但是我无法确定它是哪张表.

I'm currently using a JFrame to hold a JTabbedPane that contains multiple tables. In my class that extends JFrame and implements TableModelListener, I have an onChanged() method that takes a TableModelEvent as an argument. I can successfully obtain data from the event on the table that the event was fired from, but I can't determine which table it was.

据我了解,这不是我打算做的事情.我相信我可能需要编写自定义TableModelListenerJTable并在其中实现onChanged()方法.

From what I understand, this is not the way to do what I intend to do. I believe that I may need to write a custom TableModelListener or JTable and implement the onChanged() method there.

我该怎么做才能确定在JTabbedPane中更改了哪个JTable?我需要找到被修改的表和行.

What do I need to do to determine which JTable was changed in the JTabbedPane? I'll need to find the table and the row that was modified.

推荐答案

TableModelListenerTableModelEvent将不提供有关与模型关联的JTable的信息,因为该模型可能被多个表共享,从理论上讲.

TableModelListener and TableModelEvent won't provide information about the JTable that the model is associated with, as the model may be shared by multiple tables, in theory.

获取行与从事件获取行有关,该事件来自firstRowlastRow属性.一旦确定了模型属于您的表,就可以使用

Getting the row is matter of getting the row from the event, which comes from the firstRow and lastRow properties. Once you can establish which table the model belongs to you, you can determine the view row by using JTable#convertRowIndexToView

要找到JTable,您至少有两个基本解决方案

To find the JTable you have, at least, two basic solutions

询问存储在每个JTabbedPane中的每个表的模型,并将其与生成表模型事件的模型进行比较

Ask each table, stored in each JTabbedPane for their model and compare it with the model that generated the table model event

TableModelJTableJTabbedPane之间进行某种形式的查找,具体取决于您最终要寻找的内容

Maintain some kind of look up between the TableModel and the JTable or JTabbedPane, depending on what it's you are ultimately after

这可以通过使用某种类型的Map来实现,该键键入TableModel

This could be achieved by using Map of some kind, keyed to the TableModel

这篇关于使用JTabbedPane中的多个JTable检测onChanged()事件的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆