Is this your first time here? SwingWiki is a Java Swing Developer community site with an big archive of Swing-related usenet groups and mailing lists, but also tips, tricks and articles and book reviews written by your colleagues from around the world. If you came here through a search engine and did not find what you were looking for, make sure to check the wiki table of contents.

Multi-line JTable cells

Default JTable cell renderer implementation uses JLabels to draw cell content. JLabel does not wrap words by default, so part of the content might not be displayed if the cell holds too much text. A separate problem is automatic width adjustment1) that can also cause hiding parts of the content.

Solving this problem is easy, but there is no one-size-fits-all solution.

Use HTML to format cells

One simple solution is to use HTML content in a cell. You can create a TableModel wrapper that adds HTML content header and footer to all non-null elements of another TableModel. This will enable multi-line display in cells, but the table will not automatically resize cells to fit the content. You must define row height (use table.setRowHeight(row, height)) manually.

This solution is aplicable if most of table content is similar, and contains text that can fit in a few lines. A table with different row heights looks ugly, and is hard to read, especially if only one row contains a lot of text.

Another limitation of HTML cell content is caused by constant table cell repainting in combination with low-performance HTML rendering - cells are rendered every time you move the table content, for example. If you have a lot of data in the table, this can be very, very slow.

Also, this can lead to problems with content editing, so HTML is not a good solution for editable tables.

Define your own renderer

You can create a custom table cell renderer that uses a text component which supports word-wrapping (such as JTextArea). This might help with performance (especially if you create your own custom light-weight version), but is also effected by the same problems as the HTML solution.

Show content in tool tips

Another solution for the problem of content larger than cell space is to show the entire content in a tool tip - see Table Cell Tool Tip for an example. You can format the content using CSS body width property so it will wrap. This way, the default renderers and editors are used, table format is not ruined, and the user can see entire cell content by holding the mouse pointer over the cell for a few seconds.


Comments? Corrections? Contact us or Login to edit pages directly (registration is free and takes less than displaying a JLabel)
  howto/multi-line_cells.txt · Last modified: 2005/02/14 18:21 by (gojko)
Recent changes | RSS changes | Table of contents | News Archive | Terms And Conditions | Register | The Quest For Software++| Ruby Resources

Sedo - Buy and Sell Domain Names and Websites project info: Statistics for project etracker� web controlling instead of log file analysis