Help coderanch get a
new server
by contributing to the fundraiser
  • 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 - multiple lines in a cell AND cells not editable?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. Then I would have the cell renderer call setEditable(false) on the JTextArea which it wraps.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):

 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much...I am so much grateful to you.
 
Marshal
Posts: 79530
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AI: welcome to the Ranch

This shows how useful old threads can sometimes be.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic