Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable problem  RSS feed

 
colin shuker
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
 
Paul Clapham
Sheriff
Posts: 22185
38
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.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java 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.
 
denis sorn
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: 20893
81
Chrome Eclipse IDE Java 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:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!