JTable Display Long Text With Linebreak & Word Wrap

Today I searched how to properly display a user note with line break and word wrap in a JTable. By default the table was truncating the note display and ignoring the line breaks in the text. After scouring the internet, I found out it is possible to inject a JTextArea into a table cell by implementing TableCellRenderer. It is also possible to control the height and width of the column from inside the renderer. Below is a class that can be used in the JTable to achieve this functionality:

package com.mr.gui;

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.TableCellRenderer;

/**
 * Setups the table to properly wrap and display notes in the table
 *
 *
 * @author Paul Zepernick
 */
public class TableCellLongTextRenderer extends JTextArea implements TableCellRenderer{

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		this.setText((String)value);
        this.setWrapStyleWord(true);
        this.setLineWrap(true);    

        //set the JTextArea to the width of the table column
        setSize(table.getColumnModel().getColumn(column).getWidth(),getPreferredSize().height);
 		if (table.getRowHeight(row) != getPreferredSize().height) {
 			//set the height of the table row to the calculated height of the JTextArea
        	table.setRowHeight(row, getPreferredSize().height);
   		}

		return this;
	}

}

It is applied to the table by doing the following:

	//needs to be set on the appropriate column indicy
	myJTable.getColumnModel().getColumn(0).setCellRenderer(new TableCellLongTextRenderer ());

I found out that by using the TableCellRenderer you an actually inject any Swing component into the table. Cool stuff… I was able to use this to inject a panel with a checkbox and a button. More to come on that later…

Announcing FlatPack 3.2 | Java Delimited / Fixed File API

ObjectLab and Paul Zepernick (me) are proud to announce Flatpack 3.2. Flatpack serves as a Java tool box for writing and reading fixed width and delimited files. It is a very mature API that has been in existence since 2005.

FlatPack is currently used in the Apache Camel project and Apache ActiveMQ just to name a couple.

For those not familiar with FlatPack, I have provided a couple quick examples below. Column names in the flat files can be bound via a header record in the file, or through an XML mapping.

Example of reading a delimited file

    	Parser parser = DefaultParserFactory.getInstance().newDelimitedParser(
                new FileReader("DataFile.txt"),  //txt file or String to parse. Can Take any Reader
                ',', //delimiter
                '"'); //text qualifier

        //obtain DataSet
        DataSet ds = parser.parse();

        while (ds.next()){ //loop through file
            ds.getString("mycolumnName");
        }


Example of reading a fixed width file

	    //Obtain the proper parser for your needs
        Parser parser = DefaultParserFactory.getInstance().newFixedLengthParser(
        		new FileReader("map.fpmap.xml"), //fixed with column map
                new FileReader("DataFile.txt"));  //txt file or String to parse.  Can take any Reader

        //obtain DataSet
        DataSet ds = parser.parse();

        while (ds.next()){ //loop through file
            ds.getString("mycolumnName");
        }

Please check out the FlatPack website for more examples and complete documentation, or download now from Sourceforge.

Go back to top