java 在 JTable 中的单元格上方显示工具提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2193762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:58:49  来源:igfitidea点击:

Show a tooltip above a cell in a JTable

javaswingjtabletooltip

提问by Damien

I need to show a tooltip above (or below :) a cell when the user enter a wrong value in it (see the image below). I have a tooltip, but I need a Point to display it at the right position, so I want to get a cell position. Do you know how to get this?

当用户在单元格中输入错误值时,我需要在单元格上方(或下方:)显示工具提示(见下图)。我有一个工具提示,但我需要一个 Point 来将它显示在正确的位置,所以我想获得一个单元格位置。你知道如何得到这个吗?

BUT, if you have a better solution to realize this behaviour, I'm open to all proposition (especially for the fact that the tooltip is not bind with the cell/Jtable/Panel and if I move/close/minmize my window the tooltip is display at the same position)

但是,如果你有更好的解决方案来实现这种行为,我对所有提议持开放态度(特别是对于工具提示未与单元格/Jtable/Panel 绑定的事实,如果我移动/关闭/最小化我的窗口工具提示显示在同一位置)

alt text

替代文字

Thanks, Damien

谢谢,达米安

采纳答案by Vinodh Ramasubramanian

回答by Dzung Nguyen

Please refer below code snippet, and you'll get the solution

请参考以下代码片段,您将获得解决方案

JTable table = new JTable() {
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component c = super.prepareRenderer(renderer, row, column);
        if (c instanceof JComponent) {
            JComponent jc = (JComponent) c;
            jc.setToolTipText(getValueAt(row, column).toString());
        }
        return c;
    }
};

If you want to only show the specific cell, all you have to do is change the column param in the params of getValueAt(...) method to a specific column which contains that cell

如果您只想显示特定单元格,您所要做的就是将 getValueAt(...) 方法的 params 中的列参数更改为包含该单元格的特定列

回答by Romain Linsolas

You have an example of such featurein the Swing components visual guide.

您在 Swing 组件视觉指南中提供了此类功能的示例

Edit: In fact, it is not really a tooltip that you need here, as the tooltip need to have the cursor positionned over the cell. You want to display the tooltip even if the cursor is outside the cell, right?

编辑:实际上,这里并不是您真正需要的工具提示,因为工具提示需要将光标定位在单元格上。即使光标在单元格之外,您也想显示工具提示,对吗?

Anyway, an alternative solution is to change the background of the cell when the value entered by the user is invalid (in orange or red for example), and then add a "real" tooltip (using the link I provided) in order to give the user a complete error message.

无论如何,另一种解决方案是在用户输入的值无效(例如橙色或红色)时更改单元格的背景,然后添加“真实”工具提示(使用我提供的链接)以便给出用户完整的错误信息。

回答by user3223087

Just use below code while creation of JTable object.

只需在创建 JTable 对象时使用以下代码。

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 {
                    //comment row, exclude heading
                    if(rowIndex != 0){
                      tip = getValueAt(rowIndex, colIndex).toString();
                    }
                } catch (RuntimeException e1) {
                    //catch null pointer exception if mouse is over an empty line
                }

                return tip;
            }
        };

回答by Patrick

Use the following code to get the value for the correct row if using RowSorter:

如果使用 RowSorter,请使用以下代码获取正确行的值:

jc.setToolTipText(getValueAt(convertRowIndexToModel(row), column).toString());