Extreme Java When vanilla Java is not enough

31Mar/080

Tooltip on JTable cells (take 2)

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;
}
Tagged as: , No Comments
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.

Tagged as: , 1 Comment