10

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

Community
  • 1
  • 1
Aequitas
  • 2,205
  • 1
  • 25
  • 51
  • `getTableCellRendererComponent` returns `Component`, it's this component onto which you want to set the tooltip, from within the `getTableCellRendererComponent` method – MadProgrammer Jul 23 '15 at 01:52
  • For [example](http://stackoverflow.com/questions/15208884/java-is-it-possible-to-put-an-image-and-a-string-in-the-same-jtable-cell/15209900#15209900) and [example](http://stackoverflow.com/questions/31238676/tooltip-position-for-cell-in-jtable/31238769#31238769) – MadProgrammer Jul 23 '15 at 01:55
  • @MadProgrammer that 2nd link is perfect, thank you! if you quickly write up an answer I'll accept it. thanks :) – Aequitas Jul 23 '15 at 02:07
  • If it solves your problem, I'll just mark it as a duplicate question ;) – MadProgrammer Jul 23 '15 at 02:08
  • @MadProgrammer yeh it did, only difference was that it's necessary to extend the DefaultTableCell**Header**Renderer, since even if you set a JTable renderer to x, the header will have its own renderer y – Aequitas Jul 23 '15 at 02:10
  • 1
    See the section from the Swing tutorial on [Specifying Tooltips For Column Headers](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#headertooltip). 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. – camickr Jul 23 '15 at 03:12
  • @camickr but if you specifically attach a renderer to the tableHeader won't it always use that regardless of the Look and Feel? – Aequitas Jul 23 '15 at 03:13
  • @camickr testing shows you are correct, changing LAF results in no more tooltips. Good pick up! – Aequitas Jul 23 '15 at 03:14
  • @Aequitas, even if you installed your custom renderer for each LAF it would not look correct. The Window table header is different than the MAC table header. – camickr Jul 23 '15 at 03:16
  • @camickr macs are not an issue in my case. So with "metal laf" my tableHeader uses defaultTableCellHeaderRenderer, what does it use with the other ones? I'm having trouble understanding that link, is it saying to create my own TableHeader? Would I still be able to use the text under the mouse as the tooltip? – Aequitas Jul 23 '15 at 03:20
  • @Aequitas, LAF independent solutions should always be used whether they are an issue now or not. See my edited answer. – camickr Jul 23 '15 at 03:30
  • @camickr Can you take a look at my answer to see if it works ok for my purposes in regards to the possible Look and Feel issue? – Michael K Sep 30 '16 at 21:07

2 Answers2

10

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

I came across this since it was similar to what I needed - I wanted to put in tooltips for the column headers. The Oracle demo example linked by camickr enabled tooltips by additional code in the JTable creation. That example steered me in the right direction, and I got it working similarly, but that way of doing it was initializing a new JTable every time the table was updated. Before, I was just using myJTable.setModel() to update the table. Plus the Oracle example looked messy and was confusing for a bit there. I didn't need to extend AbstractTableModel since it didn't look like it affected tooltips at all.

So how could I get column header tooltips without making a new JTable each time and without the mess? The crucial code in the JTable initialization was overriding a method in the JTable "protected JTableHeader createDefaultTableHeader()" which of course allows for a table header (JTableHeader) with tooltips. The JTableHeader is what I really wanted to work on.

What I did is I created a new class that extended JTableHeader so that it included a tooltips String array in the constructor and a getToolTipText() method (same as the example except with out the String tip), and then I did myJTable.setTableHeader() to set it to an instance of my new class that has the tooltips String array.

(I'm posting this as an answer since it's too involved for a comment, but could be useful to others)

Here is the code in my GUI class when I update the table-

myJTable.setModel(new javax.swing.table.DefaultTableModel(
            tableData,
            colHeader
        ));//setting the new data and col headers! (no tooltips yet)

MyTableHeader headerWithTooltips = new MyTableHeader(myJTable.getColumnModel(), colHeaderTooltips);//make a new header that allows for tooltips
myJTable.setTableHeader(headerWithTooltips);//use that header in my table

And here is my MyTableHeader class-

class MyTableHeader extends JTableHeader {

    String[] tooltips;

    MyTableHeader(TableColumnModel columnModel, String[] columnTooltips) {
      super(columnModel);//do everything a normal JTableHeader does
      this.tooltips = columnTooltips;//plus extra data
    }

    public String getToolTipText(MouseEvent e) {
        java.awt.Point p = e.getPoint();
        int index = columnModel.getColumnIndexAtX(p.x);
        int realIndex = columnModel.getColumn(index).getModelIndex();
        return this.tooltips[realIndex];
    }
}
Michael K
  • 1,031
  • 2
  • 14
  • 27
  • `I thought it was strange to create a new JTable every time the table is updated.` - there is no need to create a new JTable. Not sure where you got that idea from. You either update the current model or set a new model for the table. – camickr Oct 01 '16 at 00:37
  • @camickr Well I had followed the example and that's what it did when it enabled tooltips (ok it only did once, but it didn't help figuring out how to adjust tooltips with new table data and header). It's done within the JTable creation. --- JTable table = new JTable(new MyTableModel()) { ... I'll reword it in my answer above. So does my answer look ok? – Michael K Oct 01 '16 at 11:44
  • `but it didn't help figuring out how to adjust tooltips with new table data and header` - you don't need to do anything. That code is now part of the JTable class and will be executed if the TableModel is ever changed. – camickr Oct 01 '16 at 13:13