如何向 jtable 中的单元格添加工具提示?

问题描述 投票:0回答:4

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

java swing jtable tooltip listener
4个回答
50
投票

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

                return tip;
            }
        };

34
投票

我假设您没有为路径编写自定义

CellRenderer
,而只是使用
DefaultTableCellRenderer
。您应该子类化
DefaultTableCellRenderer
并在
getTableCellRendererComponent
中设置工具提示。然后设置该列的渲染器。

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
...

0
投票

你说你在单元格中存储了绝对路径。您可能正在使用

JLabel
来设置 绝对路径字符串。假设你的单元格中有一个标签,使用 html 标签来表达工具提示内容:

JLabel label = new JLabel("Bla bla");
label.setToolTipText("<html><p>information about cell</p></html>");
如果您使用的是 JLabel 之外的其他组件,

setToolTipText()
可用于其他一些 Swing 组件。


0
投票

但是此解决方案需要定制:

  • 仅在显示省略号时向基于文本的组件添加工具提示。
  • 可以处理非文本组件,如 ImageIcon。
  • 还可以处理 DateColumn 和 HintDate 等自定义组件。

代码:

package com.example.util;

import com.example.DateColumn;
import com.example.NavTreeUserObject;
import com.example.HintDate;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;

public class TooltipsTable extends JTable
{
    public String getToolTipText(MouseEvent e)
    {
        String tip = null;
        String value = null;

        // get the row & column from the mouse point
        java.awt.Point point = e.getPoint();
        int row = rowAtPoint(point);
        int column = columnAtPoint(point);

        // within the dataset?
        if (row >= 0 && row < getRowCount() &&
                column >= 0 && column < getColumnCount())
        {
            // get the object
            Object object = getValueAt(row, column);
            if (object != null)
            {
                // get value for tooltip
                if (object instanceof String)
                    value = (String) object;
                else if (object instanceof HintDate)
                    value = object.toString();
                else if (object instanceof DateColumn)
                    value = object.toString();
                else if (object instanceof NavTreeUserObject)
                    value = ((NavTreeUserObject) object).name;

                if (value != null)
                {
                    // get the widths of text & column
                    Component component = getComponentAt(row, column);
                    FontMetrics metrics = component.getFontMetrics(component.getFont());
                    int textWidth = SwingUtilities.computeStringWidth(metrics, value);
                    int columnWidth = getColumnModel().getColumn(column).getWidth();

                    // will the text fit, i.e. is the ellipsis showing?
                    if (columnWidth < textWidth)
                        tip = value;
                }

                // get the description from an ImageIcon for the tooltip
                if (object instanceof ImageIcon)
                    tip = ((ImageIcon) object).getDescription();
            }
        }

        return tip;
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.