• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

TableModelListener firing twice.

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings, all.

Back with another prob, possibly?? I have an MVC app I'm working on & have added a JTable with a TableModel I've extended from AbstractTableModel. I've added a TableModelListener() to the Controller class to see when a JCheckBox has changed which is the only editable column in the JTable.

For some reason, the TableModelListener ends up firing twice. The first time it returns whatever the value was before clicking the JCheckBox. The second time, it ends ups returning the current value. Both calls register as an "UPDATE" event.

Is it normal for the TabelModelListener to be called twice from one change? If not, any hints to what might be causing this? I'll try to find the relevant code, put it takes some poking around with this mvc app & around 35K lines of code.

Thanks!

-Jay
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show us your table model code?
 
Jw Jones
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Can you show us your table model code?



Here is my complete table model code:



Please forgive all the crazy System.out... testing junk.
 
Jw Jones
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Narrowed it down a bit further. If I check/uncheck the JCheckBox with the spacebar, it only fires once. If I check/uncheck it with a left-mouse click, it fires twice. Strange...
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you are only calling fireTableCellUpdated once, it seems that setValueAt is called twice. I have no idea why though.

I did notice two... odd coding practices:
1) !(someRef == null)
Why not just write someRef != null?

2) colName.getClass().getSimpleName().equalsIgnoreCase("String")
If instanceof is not good, you can still use a simpler trick:

It's shorter, faster and easier to read.
 
Jw Jones
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Since you are only calling fireTableCellUpdated once, it seems that setValueAt is called twice. I have no idea why though.

I did notice two... odd coding practices:
1) !(someRef == null)
Why not just write someRef != null?

2) colName.getClass().getSimpleName().equalsIgnoreCase("String")
If instanceof is not good, you can still use a simpler trick:

It's shorter, faster and easier to read.



Thanks for the coding tips, Rob. Your way does seem much easier to read.

I have removed any MouseListener that I know of, but I am still having getColumnClass & getValueAt methods called from the TableModel just by mousing over a row in the table. This isn't normal without a mouse listener for JTables, is it?
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i tried your code though setValue is called ONCE and getValue or getColumnClass are called sometimes twice or by hovering mouse because the JTable will check for toolTipText by constructing renderer component
 
Jw Jones
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured it out. For some reason I was removing & re-adding the mouse listeners for the JTable. With some of the changes I had made, I suppose it ended up duplicating them causing this funky behavior.
 
reply
    Bookmark Topic Watch Topic
  • New Topic