How to obtain the coordinates of the JTable cell a mouse is hovering over, useful for tooltips within cell renderers.

    public String getToolTipText(MouseEvent event) {
        /*
         * This is a Java Swing lesson on how to obtain the coordinates of the current cell
         * as you hover with the mouse on a JTable.
         * 
         * You cannot use the renderer component, since it seems that once the table is done
         * stamping the cells with it, the instance of the renderer is not the one under the mouse
         * as it will always yield negative coordinates, for example, our debugger showed that this
         * renderer's coordinates were always (-95,-25)...
         * 
         * What we did in this case, to show labels for the specific components inside the renderer
         * was to get the mouse coordinates, and translate its coordinates to the coordinates of the
         * current Cell Rectangle.
         * 
         * One interesting gotcha in the process is that you cannot alter the event coordinates and then
         * try to use event.getPoint() since event.getPoint() will always give you a new instance of Point
         * so we keep a copy of that Point and then translate that point.
         * 
         * tags: java, swing, table, get current cell coordinates, get table cell coordinates under mouse
         */
        try {
            Component[] components = this.getComponents();
            Point p = event.getPoint();
            int row = table.rowAtPoint(p);
            int col = table.columnAtPoint(p);
            Rectangle currentCell = table.getCellRect(row, col, false);
            p.translate(-currentCell.x, -currentCell.y);

            for (Component c : components) {
                JComponent jc = (JComponent) c;
                if (jc.isVisible() && jc.getBounds().contains(p)) {
                    return jc.getToolTipText(event);
                }
            }
        } catch (Throwable t) {
            //don't risk painting the table over a tooltip
        }
        return super.getToolTipText(event);
    }

One thought on “How to obtain the coordinates of the JTable cell a mouse is hovering over, useful for tooltips within cell renderers.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.