2

这是我的应用程序的图像:

在此处输入图像描述

如您所见,其中有一个 JTable。我需要获取 JTable 的标头相对于它所在的 JFrame 的位置。

为什么我会这样做?我正在添加一个帮助功能来解释表中每一列的用途。它把焦点放在每一列上。它看起来像这样:

在此处输入图像描述

如您所见,聚光灯显示的位置已关闭。

要获取位置,我Rectangle使用以下方法获取列标题:

getTable().getTableHeader().getHeaderRect(i); 

要将其转换Rectangle为 JFrame 中的正确位置,我使用:

Point point = SwingUtilities.convertPoint(
                component, //the containing JPanel
                rectangle.getLocation(), //the rectangle for the table column header
                frame.getGlassPane()); //the containing JFrame

有人可以帮我获取列标题的正确位置吗?

4

3 回答 3

2

我认为您需要将表用作源组件:

Point point = SwingUtilities.convertPoint(
            getTable(), //the table as reference
            rectangle.getLocation(), //the rectangle for the table column header
            frame.getGlassPane()); //the containing JFrame
于 2015-02-25T16:58:02.473 回答
1

我想到了。这是两个问题的组合。首先,感谢 Uli 的回答。传球是正确的举动。其次,当表格在屏幕上不可见时,我正在计算矩形。这产生了不正确的坐标。

显示表格后,我将其切换为 Rectangle,这是正确的。

于 2015-02-25T17:51:10.023 回答
0

我根本不是 Java 专家,但就在昨天,我正在阅读 Oracle 文档,显然已经有一些方法可以解决这个问题(getToolTipText(MouseEvent))。他们还提出了一种替代方法来根据内容在 JTable 的标题上显示帮助。我建议你看一下:

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

于 2015-03-09T14:02:38.270 回答