如何在JTable上添加工具提示以在mouseover事件上显示特定列的单元格值

时间:2012-10-29 12:23:52

标签: java swing jtable tooltip mouseover

  

可能重复:
  How to add tooltips to JTable’s rows

我想在给定特定列下的单元格上为用户鼠标悬停事件添加工具提示,以便将单元格的值完全显示给用户。

2 个答案:

答案 0 :(得分:4)

我认为覆盖prepareRenderer()对于单个单元格来说可能更容易

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;
    }
};

答案 1 :(得分:0)

只需解决单元格渲染器的工具提示

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;
    }

}