• 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 cellEditable issue

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic