• 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

JTable problem

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The TableModel should take care of editability. Check out its isCellEditable method.
 
denis sorn
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to override isCellEditable method in TableModel. If you want to make all cells uneditable this would work:

 
reply
    Bookmark Topic Watch Topic
  • New Topic