Java Tips Weblog

  • Blog Stats

    • 1,103,020 hits
  • Categories

  • Archives

Left Dot Renderer

Posted by Rob Camick on November 12, 2008

The default renderer for a JTable uses a JLabel to provide the rendering. When the text on the label is truncated, the text is displayed left justified with trailing dots. There may be occasions when the trailing part of the text is more important than the start of the text and you would rather have the text right justified with leading dots. We can’t easily change the default behavour of a label, but we can change the behavour of the renderer.

Of course you could just increase the column width to see all the text and that is now easier thanks to the TableColumnAdjuster described in an earlier entry. However, this entry is about customizing the rendering. The LeftDotRenderer will display the text normally in the table column as long as the text fits in the width of the table column. When the text is too long to display completely, the renderer will determine the number of characters that can be displayed and then insert “…” at the start of the text to give the desired effect of right justifying the renderer.

To install the renderer of a given column you would use code like…

LeftDotRenderer leftDot = new LeftDotRenderer();
table.getColumnModel().getColumn(1).setCellRenderer(leftDot);

… and the table would display like:

left-dot-renderer

Get The Code

LeftDotRenderer.java

See Also

Table Column Adjuster

Related Reading

Concepts: Editors and Renderers

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 88 other followers

%d bloggers like this: