Posts Tagged ‘jtable’

Tooltip on JTable cells (take 2)

Monday, March 31st, 2008

In a previous post, I explain how to add tooltips to JTable cells. Armand Bendananff told, in a comment, that a better way is to override the table’s getToolTipText method 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";
  return tooltip;
}

Tooltip on JTable cells

Thursday, February 14th, 2008

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.