如何向JTable的行添加工具提示 [英] How to add tooltips to JTable's rows

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

问题描述

如何向JTable的行添加工具提示(Java Swing)?
这些工具提示应该包含相对行的相同值。

how can I add tooltips to JTable's rows (Java Swing)? These tooltips should contain same values of the relative row.

这是我在我的类中使用的扩展JTable的代码。它覆盖了方法prepareRenderer,但是我得到了空单元格,它为行内的每个单元格添加了一个工具提示,而不是整行的一个工具提示(这就是我正在寻找的):

This is the code I used in my class that extends JTable. It overrides the method "prepareRenderer", but I got empty cells, and it adds a tooltip for each single cell within row, not one tooltip for the whole row (that is what I'm looking for):

public Component prepareRenderer(TableCellRenderer renderer,int row, int col) {
    Component comp = super.prepareRenderer(renderer, row, col);
    JComponent jcomp = (JComponent)comp;
    if (comp == jcomp) {
        jcomp.setToolTipText((String)getValueAt(row, col));
    }
    return comp;
}


推荐答案


它为行内的每个单元格添加工具提示,而不是为整行添加一个工具提示

it adds a tooltip for each single cell within row, not one tooltip for the whole row

您正在更改工具提示,具体取决于行和列。如果您只希望工具提示按行更改,那么我只会检查行值并忘记列值。

You are changing the tooltip depending on the row and column. If you only want the tooltip to change by row, then I would only check the row value and forget about the column value.

设置工具提示的另一种方法是覆盖JTable的 getToolTipText(MouseEvent)方法。然后你可以使用表的 rowAtPoint(...)方法获取行,然后返回该行的相应工具提示。

Another way to set the tooltip is to override the getToolTipText(MouseEvent) method of JTable. Then you can use the rowAtPoint(...) method of the table to get the row and then return the appropriate tool tip for the row.

这篇关于如何向JTable的行添加工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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