Search code examples
javaswingjtabletooltip

Tooltip text for each column of a JTable header


I am trying to display the text in each cell of the header as a tooltip when you hover over that cell.

I have found that you can set the tooltip for the entire header: table.getTableHeader().setToolTipText("asdf"); but cannot do similar for each cell such as: table.getTableHeader().getColumnModel().getColumn(0).setToolTipText("asdf");

I have looked at this question but cannot understand how to override getToolTipText when the only method in TableCellRenderer is getTableCellRendererComponent.

The only class that I've found that hass this getToolTipText is JComponent


Solution

  • See the section from the Swing tutorial on Specifying Tooltips For Column Headers.

    I would recommend this approach because each LAF could have its own custom renderer, so extending the default renderer won't work for all LAF's.

    The Windows table header is different than the MAC table header which is different than the Nimbus table header.

    is it saying to create my own TableHeader?

    It is overriding the code that creates the JTableHeader so you can override the getToolTipText(MouseEvent) method of the JTableHeader so you can provide your own tooltip based on the mouse location. The example code just gets the tooltip from an Array.

    Would I still be able to use the text under the mouse as the tooltip?

    If you want the text of the header you need to get the TableColumnModel from the JTableHeader, then get the TableColumn and then use getHeaderValue() to get the text of the column header.