Search code examples
javaswingjtable

Presenting multiple lines of text in JTable column


I have a JTable, I Want to present a String of text containing "\r\n" which represent new lines. And I want these new lines to show in the table and not just be ignored. A visual representation of my problem follows.

Today:

|Titel           |
------------------
|texttex ttext xt|
------------------

Goal:

  |Titel   |
  ----------
  |texttex |
  |ttext xt|
  ----------

I have experimented with JTextPane but my result is a textual representation of that object instead of a text encapsulated in a JTextPane..(Maybe I don't understand all those obvious internet guides).

To summarize the question, how do I present a column in a JTable which contains multiple lines without having a indexation for each line. (or basic instructions of how to really make the JTextPane work in a column..?)


Solution

  • I would suggest either you use <br> instead of \r\n or you set a TableCellRenderer on your JTable. The TableCellRenderer would return a JTextPane when it is called - create only one instance of JTextPane and return it for each call (after having set the appropriate value)