Java custom JTable handling multiple TableModels

39 views Asked by At

Have a custom JTable class that implements dynamic tooltips that only appear when the ellipsis is shown for any value. Using that TooltipsTable class for several different TableModels.

Question: Is there a better approach or tighter syntax than using instanceof like this expanding it for the 11 different TableModels in my application?

public class TooltipsTable extends JTable
{
    @Override
    public String getToolTipText(MouseEvent e)
    {
        // will post if requested
    }

    @Override
    public int getRowCount()
    {
        int result = 0;
        TableModel tm = getModel();
        if (tm instanceof BrowserTableModel)
        {
            BrowserTableModel btm = (BrowserTableModel) getModel();
            result = btm.getRowCount();
        }
        else
        {
            DefaultTableModel dtm = (DefaultTableModel) getModel();
            result = dtm.getRowCount();
        }
        else
        {
            ... more models
        }
        return result;
    }
}

Looking for a tighter, more-flexible / less-specific, technique if possible.

0

There are 0 answers