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
Joined: Aug 02, 2012
Posts: 198
posted
0
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
If this is not what you meant, please post your SSCCE code which clearly demonstrates your problem.
Jacky Luk
Ranch Hand
Joined: Aug 02, 2012
Posts: 198
posted
0
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
Joined: Jun 19, 2012
Posts: 69
posted
0
- 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
- 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
Joined: Aug 02, 2012
Posts: 198
posted
0
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
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.