• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable row change event?  RSS feed

 
Samson Mlati
Ranch Hand
Posts: 41
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm displaying database rows in a JTable and wan't to detect when a user selects a different row in the list of rows. The default behaviour is that you can highlight different rows but I don't know what event to listen for or how. Any ideas? My aim is to display fields from the highlighted row in form input types so the user can update them. Thanks

Samson.
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a TableModelListener that you attach to the JTable's model. There is only one event (tableChanged) so you should check the TableModelEvent's type.
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See: How to Write a List Selection Listener
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob C is actually right. TableModelListener is for noticing changes in the data (model). A ListSelectionListener is for noticing selection changes.

That article is missing a part though. It only describes how to handle selections of entire rows. If you change the table to allow selection of single cells then you must not only add a ListSelectionListener to the JTable's ListSelectionModel, but also to the ListSelectionModel of the JTable's TableColumnModel.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!