• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get row number selected when fireTableDataChanged has occured?

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Maneesh Godbole
Saloon Keeper
Posts: 11165
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure I understand your question. How is the data change related to selection? Can you please rephrase?
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





public void fireTableCellUpdated(int row, int column) wasn't called when an update is initiated.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:I am not sure I understand your question. How is the data change related to selection? Can you please rephrase?


Hi Maneesh,
I believe I am using the wrong "fire" method, I should be using fireTableCellUpdated, however, it isn't called when an update event occurs
 
Maneesh Godbole
Saloon Keeper
Posts: 11165
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Luk wrote:
Maneesh Godbole wrote:I am not sure I understand your question. How is the data change related to selection? Can you please rephrase?


Hi Maneesh,
I believe I am using the wrong "fire" method, I should be using fireTableCellUpdated, however, it isn't called when an update event occurs

I still don't understand where the selection comes into the picture.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
Jack Luk wrote:
Maneesh Godbole wrote:I am not sure I understand your question. How is the data change related to selection? Can you please rephrase?


Hi Maneesh,
I believe I am using the wrong "fire" method, I should be using fireTableCellUpdated, however, it isn't called when an update event occurs

I still don't understand where the selection comes into the picture.


Hi Maneeh,
Actually, There are 3 JTextFields, Patient ID, Patient First Name, Patient Last Name and one combo box called Hospital Name that displays individual patient's details
And there is a table that displays the whole table information
When a user selects a row on the table, the JTextfields will be updated.
Thanks
Jack
db.png
[Thumbnail for db.png]
My Application that updates the JTextField when user clicks and changes the info on the JTable
 
Maneesh Godbole
Saloon Keeper
Posts: 11165
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Luk wrote:
When a user selects a row on the table, the JTextfields will be updated.

So why are you firing the table change? You should be listening to the table selection.
Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#selection

If this is not what you meant, please post your SSCCE code which clearly demonstrates your problem.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hello, I am getting there. But 2 mistakes I have made, since there is a "delete" button available, when I click on it, the row will be deleted instantly, afterwards, this rowlistener will be invoked.
Since all CRUDs is a valueChanged activity, outputSelection will be called each time a CRUD starts, 2 problems, first is the row is deleted, it is no longer available in the table, there will be a arrayindexoutofbounds
Exception thrown, also, not all activities are update activities, db.updatePatientData(savedValues) should not be there too.
Any insights?
Thanks
Jack
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- all your posts here about to create DefaultTableModel is wrong, all these methods are implemented and correctly, no idea, no reason to override that

- never to call fireXxxXxx for DefaultTableModel, these methods are implemented and correctly

- for why reason you override public void addRow(Vector rowData) { and public void addRow(Object[] rowData) { inside one XxxTableModel

- you are using these Object wrong

- (even model is created before) you called DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); in outputSelection() { then you have to lost reference betweens GUI and model

- even valueChanged() from ListSelectionListener fired the proper event, Id be to test if (row> -1)

- have to change ListSelectionModel to SINGLE_Xxx, then isnt possible to select more than one row

- database whatever (db.updatePatientData(savedValues);) must be based on PrepareStatement

- be sure that PrepareStatement.close() (in finally block), otherwise these Object never gone from JVM memory, nor GC'ed
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I am back, when I press the delete button, the following things happen
1) RowListener invoked
2) Delete actionListener invoked
3) removeRow invoked
4) RowListener invoked again








Result:
Connected!
Updating row 4
deleting row 4
Updating row -1
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementData(Vector.java:730)
at java.util.Vector.elementAt(Vector.java:473)

I found the program has an execution sequence problem. Any insights? The updating process has been done twice
However, I'd like to keep the RowListener as I want to update the table interactively.
Thanks
Jack
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, when you want to delete a row, you should update the 'model' by removing the row and then fire the appropriate method to notify the table rather than repaint it.
Since you are extending DefaultTableModel, you can call the removeRow() method of the model to do this (the removeRow automatically calls the fireXXX method to notify the table).

Secondly, you should also consider the functional aspect when you delete a row. A row is selected and you click the delete button. Now, after the row is deleted, what should happen with the selection? should the next row get selected? should the previous row get selected? what happens in case the deleted row is first/last in the table?

I think when a row is deleted, the selection is lost. So, the getSelectedRow() returns -1 which is what is happening in your case. So, you should simply add an if condition in your outputSelection() method to handle this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic