开发者

How to force a tooltip to update on a TableCellRenderer, when the text is identical?

开发者 https://www.devze.com 2022-12-10 12:14 出处:网络
In a Java program, I am using a custom renderer for cells in a JTable. On this renderer I set a tooltip, for which the content depends on the current cell.

In a Java program, I am using a custom renderer for cells in a JTable. On this renderer I set a tooltip, for which the content depends on the current cell.

When the values are different, the tooltip is updated, and will appear next to the mouse pointer, over the cell.

However, when the text for this tooltip is identical when changing cell (it happens that a few cells have the same text for tooltip), the TooltipManager considers that the tooltip hasn't changed, and it leaves the previous one, on the previous position.

开发者_如何学CDoes someone knows how to make it so that the tooltip would be updated on each cell, even with identical values?


I think your best bet is to override getToolTipLocation(MouseEvent) in your component, and have it track the location of the mouse. If either the text or the location of a tooltip have changed, then the tooltip will update.


Add or remove a zero-width, non-breaking space?</hack>


Try to use CellStyle for tooltips update:

@Override
public CellStyle getCellStyleAt(int row, int column) {
        Object property = super.getPropertyAt(row);
        String description = property instanceof Property ? ((Property) property).getDescription() : null;
        if (!StringUtils.isBlank(description)) {
            if (description.length() > splitLength) { // Automatically split long descriptions, store results in cache map.
                String splitString = descriptionToSplit.get(description);
                if (null == splitString) {
                    splitString = StringUtils.splitToRows(description, splitLength);
                    descriptionToSplit.put(description, splitString);
                }
                cellStyle.setToolTipText(splitString);
            } else { // Optimization for short descriptions.
                cellStyle.setToolTipText(description);
            }
        } else { // If description is empty, use display name instead.
            String name = property instanceof Property ? ((Property) property).getDisplayName() : null;
            cellStyle.setToolTipText(name);
        }
        return cellStyle;
    }

    @Override
    public boolean isCellStyleOn() {
        return true;
    }

    private static final CellStyle cellStyle = new CellStyle();
0

精彩评论

暂无评论...
验证码 换一张
取 消