Tooltip text for each JTable header column

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

I found that you can set the tooltip for the entire header: table.getTableHeader().setToolTipText("asdf");

but cannot do this for every cell, e.g .:table.getTableHeader().getColumnModel().getColumn(0).setToolTipText("asdf");

I have looked into this question but cannot figure out how to override getToolTipText

when the only method in the TableCellRenderer is getTableCellRendererComponent

.

The only class I found that this getToolTipText is JComponent

+3


source to share


2 answers


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 renderer, so the default render extension would not work for all LAFs.

The Windows table header is different from the MAC table header, which is different from the Nimbus table header.

does it say to create my own TableHeader?



It overrides the rendering code, JTableHeader

so you can override the getToolTipText(MouseEvent)

JTableHeader method so you can provide your own mouse-based tooltip. The example code just gets the tooltip from the array.

Can I use the text under the mouse as a hint?

If you want header text, you have to get TableColumnModel

from JTableHeader, then get TableColumn

and then use getHeaderValue()

to get the column header text.

+3


source


I stumbled upon this as it looked like what I needed - I wanted to add tooltips for the column headers. The Oracle demo example related to camickr included tooltips with additional code in the JTable creation. This example nudged me in the right direction and I worked on it the same way, but this way is to initialize a new JTable every time the table has been updated. Previously, I just used myJTable.setModel () to update the table. Also, the Oracle example looked messy and a bit confusing. I didn't need to extend AbstractTableModel as it didn't look like it affected tooltips.

So how can I get the column header tooltips without creating a new JTable every time and without messing around? The critical code in JTable initialization was overriding the JTable-protected JTableHeader's createDefaultTableHeader () method, which of course allows the table header (JTableHeader) with tooltips. JTableHeader is what I really wanted to work on.

I created a new class that extended JTableHeader to include an array of String tools in the constructor and a getToolTipText () method (same as in the example except with a String outline) and then I did myJTable.setTableHeader (). to set it to an instance of my new class which has tooltips. String array.

(I am posting this as an answer as it is too involved in a comment, but may be helpful 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];
    }
}

      

0


source







All Articles