Question

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.

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.

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.

Was it helpful?

Solution

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.

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

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

You Could...

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

You Could...

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

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

OTHER TIPS

I believe that I may need to write a custom TableModelListener...

Check out the Table Cell Listener.

It is very similar to a TableModelListener, but you do need to specify the JTable when creating the TableCellListener, so you do have access to the table when a value is changed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top