• Post Reply Bookmark Topic Watch Topic
  • New Topic

DefaultTableModel cell editable  RSS feed

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make a table cell editable using DefaultTableModel,
why it doesn't work ?

Thanks.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I want to make a table cell editable using DefaultTableModel,

default is editable, so what are you really trying to do?
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edward Chen:
dataModel.isCellEditable(i, j); // return true

You're right - it does return true. But that's all it does. DefaultTableModel does not allow you to specify for each cell separately whether it's editable or not. As Michael said, DefaultTableModel.isCellEditable returns true regardless of the row and column.

If you want to make some cells (or entire columns) editable and some not, you should extend DefaultTableModel and override isCellEditable to return the value you need for each cell / column. For instance, using an anonymous subclass:
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what are you really trying to do?[/QB]


I want it to show up an input box after clicking a cell, so that I could enter a new value.

Thanks.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default cell editor requires two clicks to start editing. You can either retrieve it, cast it to DefaultCellEditor (after you've made sure it is one!) and decrease that count, or you can create your own cell renderer.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I want it to show up an input box after clicking a cell, so that I could enter a new value.

perhaps just a mouseListener will do what you're after

 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:> I want it to show up an input box after clicking a cell, so that I could enter a new value.

perhaps just a mouseListener will do what you're after

I'd change just one thing:

If the user presses cancel you want to avoid clearing your cell.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!