Setting Tool Tips on Cells in a JTable Component

Unfortunately, there is no setToolTipText() method for cells in a JTable component. For a cell to show a tool tip, the renderer for that cell must set the tool tip text on the returned component. See Creating a Custom Cell Renderer in a JTable Component for an example of a renderer that sets a tool tip.

If you cannot modify the renderer, you can override the table's prepareRenderer() method and explicitly set the tool tip on the returned component.

// This table displays a tool tip text based on the string // representation of the cell value JTable table = new JTable() { public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int vColIndex) { Component c = super.prepareRenderer(renderer, rowIndex, vColIndex); if (c instanceof JComponent) { JComponent jc = (JComponent)c; jc.setToolTipText((String)getValueAt(rowIndex, vColIndex)); } return c; } };

Comments

4 Mar 2010 - 10:07am by Joni (not verified)
Much simpler I think:
JTable table - new JTable(); table.addMouseMotionListener(new MouseMotionAdapter(){ public void mouseMoved(MouseEvent e){ Point p = e.getPoint(); int row = table.rowAtPoint(p); int column = table.columnAtPoint(p); table.setToolTipText(String.valueOf(table.getValue(row,column))); }//end MouseMoved }; // end MouseMotionAdapter

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.