如何将工具提示添加到jtable中的单元格? [英] How to add a tooltip to a cell in a jtable?

查看:197
本文介绍了如何将工具提示添加到jtable中的单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表,每行代表一张图片。在路径I中,存储其绝对路径。字符串有点长,我想当我将鼠标悬停在特定单元格上时,工具提示应该弹出包含来自单元格信息的鼠标旁边。

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 the specific cell, a tooltip should pop-up next to the mouse containing the information from the cell.

推荐答案

我假设您没有为路径编写自定义 CellRenderer ,只需使用 DefaultTableCellRenderer 。您应该继承 DefaultTableCellRenderer ,并在 getTableCellRendererComponent 中设置工具提示。然后设置列的渲染器。

I assume you didn't write a custom CellRenderer for the path but just use the DefaultTableCellRenderer. You should subclass the DefaultTableCellRenderer and set the tooltip in the getTableCellRendererComponent. Then set the renderer for the column.

class PathCellRenderer extends DefaultTableCellRenderer {
    public Component getTableCellRendererComponent(
                        JTable table, Object value,
                        boolean isSelected, boolean hasFocus,
                        int row, int column) {
        JLabel c = (JLabel)super.getTableCellRendererComponent( /* params from above (table, value, isSelected, hasFocus, row, column) */ );
        // This...
        String pathValue = <getYourPathValue>; // Could be value.toString()
        c.setToolTipText(pathValue);
        // ...OR this probably works in your case:
        c.setToolTipText(c.getText());
        return c;
    }
}

...
pathColumn.setCellRenderer(new PathCellRenderer()); // If your path is of specific class (e.g. java.io.File) you could set the renderer for that type
...




  • 关于工具提示的Oracle JTable教程

    • Oracle JTable tutorial on tooltips
    • 这篇关于如何将工具提示添加到jtable中的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆