Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable actionlistener not working

 
Krithika Beri
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My tables are not responding when there is any change. Please help


 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried what you have told me but it still does not seem to work
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i get the values of row, col and value?


 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much, I got it..
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic