• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Keep JTable selection when adding a row  RSS feed

 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a real-time application that adds rows to the model supporting a JTable from time to time. Every time a row is added, any user row selection is lost.

I am looking for a technique to use so that, if there is a row selected when a row is added, it remains selected after (and preferably while) a row is added.

Ideallly this will be something I can do in GUI-related classes; the part of the current code that adds a row does not have a reference to the JTable display; the former is model, the latter is view. There is currently no reference to the JTable in the code that adds a row to the model, I'm hoping I don't have to put one there to grab the selected row, add the new row, and then select the selected row again; that would seriously mix UI and modelling logic.

rc

 
Rancher
Posts: 2940
19
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding a row does not change the selection model.

You must be invoking the wrong fireXXX event. you should be using fireTableRowsInserted(...).
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ralph Cook wrote:I am looking for a technique to use so that, if there is a row selected when a row is added, it remains selected after (and preferably while) a row is added.


Rob is right. But in addition, the "preferably while" part is suspicious. Do you update the model from a background (non-GUI) thread? If so, you must properly synchronize. Otherwise, if the table gets repainted while your model is being updated, it could provide inconsistent data to JTable.

I was trying to implement background updates some time ago but then gave up. I now update models always from the GUI thread. In this case no change in the GUI will be displayed while model is updated, including changes in the table selection (row addition does not change selection, but eg. row deletion clearly can).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!