Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add tooltips to JTable's rows

how can I add tooltips to JTable's rows (Java Swing)? These tooltips should contain same values of the relative row.

This is the code I used in my class that extends JTable. It overrides the method "prepareRenderer", but I got empty cells, and it adds a tooltip for each single cell within row, not one tooltip for the whole row (that is what I'm looking for):

public Component prepareRenderer(TableCellRenderer renderer,int row, int col) {
    Component comp = super.prepareRenderer(renderer, row, col);
    JComponent jcomp = (JComponent)comp;
    if (comp == jcomp) {
        jcomp.setToolTipText((String)getValueAt(row, col));
    }
    return comp;
}
like image 845
Randomize Avatar asked Nov 30 '11 19:11

Randomize


People also ask

How do I add tooltip to a table?

Create a HTML table. Create a <span> element in the table cell in which we want to add a tooltip. Initially set the display: none of the <span> element. Whenever the user hovers over this particular element, Just change the property to display: block.

How do I add a tooltip to TD?

The title attribute on a <td> tag adds a tooltip with title text to the table data cell. Hovering the mouse over the table cell will display the tooltip.

How do I make my tooltip always visible?

Single element To make an element display its tooltip permanently, we use its showTooltipOn property. To make tooltip always be shown, set it to "always" .

How do I add a tooltip to a tag?

Via data attributes − To add a tooltip, add data-toggle = "tooltip" to an anchor tag. The title of the anchor will be the text of a tooltip. By default, tooltip is set to top by the plugin.


2 Answers

it adds a tooltip for each single cell within row, not one tooltip for the whole row

You are changing the tooltip depending on the row and column. If you only want the tooltip to change by row, then I would only check the row value and forget about the column value.

Another way to set the tooltip is to override the getToolTipText(MouseEvent) method of JTable. Then you can use the rowAtPoint(...) method of the table to get the row and then return the appropriate tool tip for the row.

like image 144
camickr Avatar answered Sep 19 '22 09:09

camickr


Just use below code while creation of JTable object.

JTable auditTable = new JTable(){

            //Implement table cell tool tips.           
            public String getToolTipText(MouseEvent e) {
                String tip = null;
                java.awt.Point p = e.getPoint();
                int rowIndex = rowAtPoint(p);
                int colIndex = columnAtPoint(p);

                try {
                    //comment row, exclude heading
                    if(rowIndex != 0){
                      tip = getValueAt(rowIndex, colIndex).toString();
                    }
                } catch (RuntimeException e1) {
                    //catch null pointer exception if mouse is over an empty line
                }

                return tip;
            }
        };
like image 38
user3223087 Avatar answered Sep 22 '22 09:09

user3223087