File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable � handle Row lost focus event

 
Sharon whipple
Ranch Hand
Posts: 294
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 886
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Brian Cole
Author
Ranch Hand
Posts: 886
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic