aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable actionlistener not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable actionlistener not working" Watch "JTable actionlistener not working" New topic
Author

JTable actionlistener not working

Krithika Beri
Ranch Hand

Joined: Sep 11, 2012
Posts: 36
My tables are not responding when there is any change. Please help


Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

What does your DetailsTableModel look like? The model is responsible for sending the modification events, do you call proper fireTable... methods when the contents of the model changes?

Edit: I'm assuming you've created the DetailsTableModel class, as it is not part of JDK.
Krithika Beri
Ranch Hand

Joined: Sep 11, 2012
Posts: 36
This is my DetailsTableModel. Could you tell me where to add the fire.... command as I have tried adding it and it is not working properly.


Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Your table model does not contain code to change it's contents. Which of the following you want to support?

1) The table model should reflect changes in the underlying structure (the node field). In this case you'll need to be able to watch for changes in the node field itself (perhaps by adding/implementing change listeners on that class) - the model would register a listener in this class call proper fireTable... method from there.

2) The table model should support edits done by the table (your implementation of the isCellEditable method suggests you want to do this). In this case, you need to override the setValueAt method too and propagate the changes to the node instance. You'd probably call fireTableCellUpdated from the setValueAt method in this case.

The Swing JTable tutorial might give you some more insight. It's an excellent resource, I suggest reading it even if you resolve your current issue without it.
Krithika Beri
Ranch Hand

Joined: Sep 11, 2012
Posts: 36
I am not still able to get my head around the problem. i already added the tableChanged method and told the model to listen to it. Then why isn't it listening?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Well, your tableChanged method is attached to your model, but the model is not sending the change events. You cannot receive an event which is not being sent.

The AbstractTableModel provides various methods you can use to send the change events (they all begin with fireTable - I've already mentioned them), but it doesn't call them iself. This is because the AbstractTableModel never changes the data it represents (it really even cannot - they are abstracted away), so you're required to implement both the support for changing the data in the model and sending proper change events when it happens.

If the data the model is based on can change independently of the model (the first option in my previous post), the model needs to get to know about these changes first and then to advertise the changes using change events - again, you need to implement both of these two functionalities yourself.

Have you read the JTable tutorial? Was there anything unclear to you?
Krithika Beri
Ranch Hand

Joined: Sep 11, 2012
Posts: 36
I have tried what you have told me but it still does not seem to work
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

If you changed your code, post what you have now. It might help if you describe the mechanism responsible for changes in the data (ie. is it only the editing done by the table, or something different).
Krithika Beri
Ranch Hand

Joined: Sep 11, 2012
Posts: 36
How do i get the values of row, col and value?


Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

The setValueAt method is called by the JTable when user finishes editing a value in a cell. Normally you don't call this method. The JTable knows what is the value the user has entered, and what are the coordinates of the cell, and it will call the setValueAt method of the model with proper values of there parameters.

Your implementation has to make sure that the change will be made in the model's data. So you need to obtain the item corresponding to the row and set its value to the value passed to the setValueAt method.

The fireTableCellUpdated call you've included is OK -- and it should cause your tableChanged method to be called when you've finished editing the value in the user interface. However, as your current implementation does not change the model's data, the displayed value won't change either, obviously. Even after you successfully edit a cell, the model still contains the old, unchanged data.
Krithika Beri
Ranch Hand

Joined: Sep 11, 2012
Posts: 36
Thank you so much, I got it..
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

You're welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable actionlistener not working