How to add a tooltip to a cell in a jtable?

前端 未结 3 2120
自闭症患者
自闭症患者 2020-12-29 23:00

I have a table where each row represents a picture. In the column Path I store its absolute path. The string being kinda long, I would like that when I hover the mouse over

3条回答
  •  太阳男子
    2020-12-29 23:13

    Just use below code while creation of JTable object.

    JTable auditTable = new JTable(){
    
                //Implement table cell tool tips.           
                public String getToolTipText(MouseEvent e) {
                    String tip = null;
                    java.awt.Point p = e.getPoint();
                    int rowIndex = rowAtPoint(p);
                    int colIndex = columnAtPoint(p);
    
                    try {
                        tip = getValueAt(rowIndex, colIndex).toString();
                    } catch (RuntimeException e1) {
                        //catch null pointer exception if mouse is over an empty line
                    }
    
                    return tip;
                }
            };
    

提交回复
热议问题