14Feb/081
Tooltip on JTable cells
This is a easy one (but it is not obvious): how to define tooltips in JTable's cells. Just create a class like this one:
public class ToolTipTable extends JTable { @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component c = super.prepareRenderer(renderer, row, column); if (c instanceof JComponent) { JComponent jc = (JComponent) c; jc.setToolTipText((String) getValueAt(row, column)); } return c; } }
This example will set cells' tooltip to its own content. Very useful if you have large contents on small cells.
March 30th, 2008 - 07:10
the better way is to override the method getToolTipText of JTable as in:
public String getToolTipText(MouseEvent event) {
Point p = event.getPoint();
// Locate the renderer under the event location
int colIndex = columnAtPoint(p);
int rowIndex = rowAtPoint(p);
String tooltip = “some value based on the row/col”;
System.out.println(“getToolTipText:” +
” on:” + rowIndex + “x” + colIndex +
” ” + tooltip);
return tooltip;
}