Author: Thomas Wernitz
This is a JTable that can display multi line cells with wrapping, derived from JTextArea and which displays every row with an individual height, by calculating the wrap around in the text area.
The classes consist of MultiLineTable, MultiLineCellRenderer and MultiLineBasicTableUI. The classes are contained in the zip file you can download by following the download link at the bottom. Here's sample code that uses these classes.
/**
* Table.java
*
* Created: Mon May 17 09:41:53 1999
*
* @author Thomas Wernitz, Da Vinci Communications Ltd <thomas_wernitz@clear.net.nz>
*
* credit to Zafir Anjum for JTableEx and thanks to SUN for their source code ;)
*/
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class Table extends JFrame {
public Table(String title) {
super(title);
setSize(600, 400);
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}});
Object[][] data = new Object[][] {{"first row", "\"I was drunk last night, crawled home across the lawn. By accident I put the car key in the door lock. The house started up. So I figured what the hell, and drove it around the block a few times. I thought I should go park it in the middle of the freeway and yell at everyone to get off my driveway.\"\n -- Steven Wright"},
{"second row", "The most advantageous, pre-eminent thing thou canst do is not to exhibit nor display thyself within the limits of our galaxy, but rather depart instantaneously whence thou even now standest and flee to yet another rotten planet in the universe, if thou canst have the good fortune to find one.\n -- Carlyle"}};
Object[] columns = new Object[] {"first column", "fortune cookie"};
MultiLineTable table = new MultiLineTable(data, columns);
MultiLineCellRenderer multiLineCR = new MultiLineCellRenderer();
table.getColumnModel().getColumn(0).setCellRenderer(multiLineCR);
table.getColumnModel().getColumn(1).setCellRenderer(multiLineCR);
getContentPane().add(new JScrollPane(table));
}
public static void main(String[] args) {
Table table = new Table("Table");
table.show();
}
}
Download Source Files
Posted On: 23-May-1999