aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable � handle Row lost focus event Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable � handle Row lost focus event" Watch "JTable � handle Row lost focus event" New topic
Author

JTable � handle Row lost focus event

Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
Hi ranchers
I have swing screen with JTable of user's list and additional information besides the table that changes on each row selection.
This data is additional user data that doesn't fit the table.

When the moving from one row to another I am collecting all additional row data to map and I attach it as hidden filed to the JTable model.
Finally when the screen is submitted, all data is flushed to the db.


My question is how I listen to the "row lost focus" and "row got focus" event in order to initiate the additional data according to the selected row, and save the data when moving to the next row

Thank you
Sharon
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Sharon whipple:
JTable ...
My question is how I listen to the "row lost focus" and "row got focus" event


The typical way is something like this:



bitguru blog
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

But Brian, won't that only show you the selected row(s)? I think Sharon needs the row that lost focus. I am assuming she just needs to keep track of that herself? For example, once the first row becomes selected, save that as a selected row in some other data structure. When the next row is selected, find the last selected row in the data structure before adding the newly selected row. Then she would know when row was deselected?


GenRocket - Experts at Building Test Data
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Gregg Bolinger:
But Brian, won't that only show you the selected row(s)? I think Sharon needs the row that lost focus. I am assuming she just needs to keep track of that herself?


Pretty much yes. You can actually query the ListSelectionEvent for the range of rows that may have changed [via e.getFirstIndex() and e.getLastIndex()] and can ask the table [via yourTable.isRowSelected() or other ways] if each row in the range is selected. Depending on your needs you can sometimes treat the selected rows in the range as new selections and unselected rows in the range as new deselections, but it's certainly possible that some of the rows in the middle of the range have not changed selection state. To get things perfect you have to keep track of the rows yourself somehow. If you're going to be doing this a lot you may want to create a custom subclass of DefaultListSelectionModel.

[edit: custom subclass of DefaultListSelectionModel, not ListSelectionModel since that's an interface]
[ January 01, 2008: Message edited by: Brian Cole ]
Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
Originally posted by Brian Cole:

[edit: custom subclass of DefaultListSelectionModel, not ListSelectionModel since that's an interface]



I DefaultListSelectionModel and I'm saving the last row when the selection is changed.

Working great!!
Thank you
Sharon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable � handle Row lost focus event