• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable - multiple lines in a cell AND cells not editable?

 
Olya Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a bit of a problem.

I'm using JTable for displaying a calendar.

It is important that more than 1 line is displayed in a cell. To do this, I wrote class MultiLineCellRenderer that extends JTextArea and implements TableCellRenderer (solution I found on the Internet).

Then I realised that the cells should not be editable. For this, I wrote class MyTableModel that extends AbstractTableModel (again, solution from the Internet).

So I apllied both to the table, like this:

table.setModel(new MyTableModel(data, columnNames));
table.setDefaultRenderer(String.class, new MultiLineCellRenderer());

The problem: It looks like I can use either one or the other. If I use the renderer for my table, multiple lines are shown in cells, but the cells are editable. If I use the table model, or both, then the cells are not editable, but only 1 line is displayed in each cell :-/

Any suggestions? Can the renderer and the table model be used together?

Thanks in advance!
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't say whether your TableModel overrides the isCellEditable() method.
 
Olya Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You didn't say whether your TableModel overrides the isCellEditable() method.


Sorry - yes, it does, and it returns false for all cells.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Then I would have the cell renderer call setEditable(false) on the JTextArea which it wraps.
 
Rob Camick
Ranch Hand
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can the renderer and the table model be used together?


Yes.

For this, I wrote class MyTableModel that extends AbstractTableModel (again, solution from the Internet).


There is no need to create a new TableModel for this. You can extend the DefaultTableModel and override the isCellEditable(...) method. Or you can extend JTable and do the same.



Did you also override the getColumnClass() method of the TableModel? The default is to return Object, so your renderer would never be used.

If you need more help then post your SSCCE that demonstrates the problem because we can't keep guessing what you custom code may or may not look like.
 
Olya Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Okay. Then I would have the cell renderer call setEditable(false) on the JTextArea which it wraps.


I called setEditable(false) in the renderer. Not sure if I've put it in the right place. Will post my code in the next comment.
 
Olya Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here is my code (sorry I thought the answer to my question might be trivial, so didn't post the code in the first place):

 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags next time; I've added them for you this time.

Rob C was right. You specified the renderer for String.class, but your table model says the column's class is Object.class.
 
Olya Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Please UseCodeTags next time; I've added them for you this time.

Rob C was right. You specified the renderer for String.class, but your table model says the column's class is Object.class.


Wow! It works!

I changed



to



and it works now!

Thank you guys very much!

P.S.
will use tags next time, thanks.
 
Olya Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:

Did you also override the getColumnClass() method of the TableModel? The default is to return Object, so your renderer would never be used.



You were right - that was the problem! Thank you for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic