Overview Basic version Model Cell Renderer Editor Column widths Tooltips Controls in cells

A tooltip for a cell can simply be set up in the cell renderer. If this is a JLabel, we can just set the tooltip of it.

For example, we will produce this:

by having in the cell renderer:

class MyRenderer extends JLabel implements TableCellRenderer {

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        setText((String) value); 
        String text = "Row = "+row+" and column = "+column;
        return this; 


A tooltip for a header can be set simply like

To set different tooltips for different columns, you can use a sub-classed JTableHeader, and over-write the getToolTipText method, with code to work out which column it is. The table header is then

class MyTableHeader extends JTableHeader {
    MyTableHeader(TableColumnModel tcm)
    public String getToolTipText(MouseEvent e) { 
        String text;
        java.awt.Point p = e.getPoint();
        int colIndex = columnAtPoint(p);
        int realColumnIndex = getTable().convertColumnIndexToModel(colIndex);
        if (realColumnIndex == 0) {
            text = "Person's name";
        } else {
            text = "Date of birth";
        return text;

The realColumnIndex.. is needed because the user can alter the order in which columns are displayed, and this works out which is the 'real' column index, ie the one in the model.

To use this, we have to get the table's column model, and pass it to the MyTableHeader constructor:

public class TestDlg extends JDialog {

    public JTable table;


    private void initComponents() {
        MyModel data = new MyModel();
        table = new JTable(data);
        table.setDefaultEditor(Date.class, new MyDateEditor());
        table.setDefaultRenderer(String.class, new MyRenderer());
        TableColumnModel tcm = table.getColumnModel();
        MyTableHeader header = new MyTableHeader(tcm);

The result, with columns swapped, is


0 responses so far

Add a new comment

Your name

Your email (will not be published)

Your comment (no HTML)

Are you a robot? Please type the two words: