• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Keep JTable selection when adding a row

 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3324
32
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic