简体   繁体   English

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

[英]Show a tooltip above a cell in a JTable

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. 我有一个工具提示,但我需要一个Point来显示它在正确的位置,所以我想获得一个单元格位置。 Do you know how to get this? 你知道怎么做吗?

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) 但是,如果你有一个更好的解决方案来实现这种行为,我对所有命题持开放态度(特别是因为工具提示没有与cell / Jtable / Panel绑定,如果我移动/关闭/最小化我的窗口工具提示显示在同一位置)

替代文字

Thanks, Damien 谢谢,Damien

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(...)方法的参数中的列参数更改为包含该单元格的特定列

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

You have an example of such feature in 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. 无论如何,另一种解决方案是当用户输入的值无效时(例如橙色或红色)更改单元格的背景,然后添加“真实”工具提示(使用我提供的链接)以便给出用户完整的错误消息。

试试getCellRect

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM