GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes JTable - multiple lines in a cell AND cells not editable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable - multiple lines in a cell AND cells not editable?" Watch "JTable - multiple lines in a cell AND cells not editable?" New topic
Author

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

Olya Smith
Greenhorn

Joined: Dec 17, 2010
Posts: 12
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You didn't say whether your TableModel overrides the isCellEditable() method.
Olya Smith
Greenhorn

Joined: Dec 17, 2010
Posts: 12
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Okay. Then I would have the cell renderer call setEditable(false) on the JTextArea which it wraps.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
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

Joined: Dec 17, 2010
Posts: 12
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

Joined: Dec 17, 2010
Posts: 12
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Olya Smith
Greenhorn

Joined: Dec 17, 2010
Posts: 12
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

Joined: Dec 17, 2010
Posts: 12
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable - multiple lines in a cell AND cells not editable?