Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 961
Aequitas Avatar asked Jul 23 '15 01:07

Aequitas


People also ask

Which method is used to add tooltip text?

We can add tooltip text to almost all the components of Java Swing by using the following method setToolTipText(String s). This method sets the tooltip of the component to the specified string s. When the cursor enters the boundary of that component a popup appears and text is displayed.

What is tooltip text in Java Swing?

Tooltips are small windows of text that popup when the user leaves the mouse cursor over a component for a second or two. They are used to explain the functionality of the component. Tooltips are an integral part of Swing components. They can be specified by calling the setToolTipText method as shown below.

How do you make a Jtable column not editable?

Right-click on the table cells. From popup menu, choose "Table Contents..". Uncheck the editable check box for the column you want to make it non-editable.


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 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.

like image 63
camickr Avatar answered Sep 23 '22 06:09

camickr


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];
    }
}
like image 33
Michael K Avatar answered Sep 23 '22 06:09

Michael K