• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tooltip for table header  RSS feed

 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Is there an easier way to set tooltips for table header ?

I found one approach here: http://javaalmanac.com/egs/javax.swing.table/ColHeadTips.html

Thanks,
Cathy.
 
Eric Snell
Greenhorn
Posts: 28
Java Mac MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The table header contains a renderer which you can set and get using
setDefaultRenderer(TableCellRenderer defaultRenderer) and TableCellRenderer getDefaultRenderer(). When getTableCellRendererComponent() is called on the renderer, you are passed the column which should allow you to determine and set the tooltip text on the renderer.

I would suggest creating an implementation of TableCellRenderer that delegates to a contained TableCellRenderer. You can get the default renderer from the table and it into your renderer. When getTableCellRendererComponent is called, set the tooltip text on the returned component. This is a few lines of code plus whatever code you use to determine your tooltip text.

You could also create your own custom render, but delegation would be much easier.
 
Eric Snell
Greenhorn
Posts: 28
Java Mac MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a code snippet I threw into a JTable subclass ctor. You can clean it up and adapt it:


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!