Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

JTable problem

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'd like my JTable to printout the row/column it is, when a user presses on a cell,
or
do something like attach a listener to the cell, so I can trigger something to happen.

Either of those will solve my problem.
Can anyone help.
Thanks
 
Marshal
Posts: 25671
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTable has a method where you can get its SelectionModel (check the API documentation for that). You can add the appropriate listener to the SelectionModel.
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And even though the selection events only return full rows, you can retrieve the selected cell from the JTable itself when an event occurs. Check out the JTable API to find which two methods to use.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add a MouseAdapter with addMouseListener to the table. In MouseAdapter class implement mousePressed method i.e. like this:



I am not sure if this is what you want but I suppose with selected row and column you should get your cell.

edit:

Yes, forgot to say, this method will only work then when you use mouse to select the cells.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the comments

The last code works well...


I also need to make the cells uneditable, but I'm not sure how exactly, its not quite like a JTextField, I can't just use setEditable(false).
Any thoughts? thanks
 
Rob Spoor
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The TableModel should take care of editability. Check out its isCellEditable method.
 
denis sorn
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to override isCellEditable method in TableModel. If you want to make all cells uneditable this would work:

 
    Bookmark Topic Watch Topic
  • New Topic