wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How to get row number selected when fireTableDataChanged has occured? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to get row number selected when fireTableDataChanged has occured?" Watch "How to get row number selected when fireTableDataChanged has occured?" New topic
Author

How to get row number selected when fireTableDataChanged has occured?

Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 580
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9995
    
    7

I am not sure I understand your question. How is the data change related to selection? Can you please rephrase?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 580





public void fireTableCellUpdated(int row, int column) wasn't called when an update is initiated.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 580
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

Joined: Jul 26, 2007
Posts: 9995
    
    7

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

Joined: Aug 02, 2012
Posts: 580
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



[Thumbnail for db.png]

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9995
    
    7

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

Joined: Aug 02, 2012
Posts: 580


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: 151
- 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

Joined: Aug 02, 2012
Posts: 580
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

Joined: Oct 16, 2003
Posts: 1075
    
  10

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.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get row number selected when fireTableDataChanged has occured?
 
Similar Threads
create a jtable which can combine and split and the image change also
regarding JTable data
Calculate sum in jtable and display the sum in jtextfield
Need Help in Modification MultiRowHeaderExample
Need Help in Modification MultiRowHeaderExample