How to add tooltip on JTable to show cell values of a particular column on mouseover event [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-12 01:57:18

问题


Possible Duplicate:
How to add tooltips to JTable’s rows

I want to add tooltip for the user mouseover event on a cell under a given particular column so that the value of the cell completely displayed to the user.


回答1:


I think overriding prepareRenderer() may be easier for a single cell

JTable table = new JTable() {
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component c = super.prepareRenderer(renderer, row, column);
        if (c instanceof JComponent) {
           if(column == X){
            //X is your particlur column number

            JComponent jc = (JComponent) c;
            jc.setToolTipText(getValueAt(row, column).toString());
           }
        }
        return c;
    }
};



回答2:


Simply settle tooltip of you of the cell renderer

public class MyCellRenderer extends DefaultTableCellRenderer {
    public Component getTableCellRendererComponent(JTable table,
                                  Object value,
                                  boolean isSelected,
                                  boolean hasFocus,
                                  int row,
                                  int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        setToolTipText(...);
        return this;
    }

}


来源:https://stackoverflow.com/questions/13121575/how-to-add-tooltip-on-jtable-to-show-cell-values-of-a-particular-column-on-mouse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!