• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable cellEditable issue

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

I have JTable with a CustomTableModel that extends DefaultTableModel.

The reason I implemented a CustomTableModel was that I didn't want the user to be able to write to individual cells.

Therefore I implemented the isCellEditable() method to always return false.




However here is my problem - I need the user to be able to copy text out of the cells.

As far as I can tell the only way I can do this is to make the cells editable - which violates my read-only requirement.

Is there any way I can satisfy both these requirements?

Thanks,

Christy.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be possible by writing your own CellEditor that returns true from isCellEditable(), but somehow ignores the input made into the editor component.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see setCellSelectionEnabled() method of JTable

setting this to true enables cell selection and allows users to copy the contents of one cell.
 
Christy Keane
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came up with this (sorta hacky) solution:

I wrote my onwn TableModelListener to listen for a table model update event, and if the user tries to change a cell's contents, I replace it with the original contents.

The user can still copy text from the cell.

 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DId you try Tim's solution? I wasn't aware of it at the time I posted, but it is obviously a much cleaner and more elegant answer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!