2

Here is an image of my application:

enter image description here

As you can see, there is a JTable in there. I need to get the location of the headers of the JTable relative to the JFrame that it is in.

Why might I be doing this? I am adding a help feature that explains what each of the columns in the table is for. It puts a spotlight on each column. It looks like this:

enter image description here

As you can see, the location where the spotlight is showing is off.

To get the location, I get the Rectangle of the column header using:

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

To convert this Rectangle to the correct location in the JFrame, I use:

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

Can someone help me with getting the correct location of the column header?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user489041
  • 27,916
  • 55
  • 135
  • 204
  • read Oracle tutorial - how to use Tables, part about ToolTip, as aside GlassPane cant covers whole RootPanes area, put JLabel to Glasspane, I'm sure that funny variation How to create a telescope in Swing by @Andrew Thompson is a few times here – mKorbel Feb 26 '15 at 13:10
  • too much borders for this view – mKorbel Feb 26 '15 at 13:12

3 Answers3

2

I think you need to use the table as source component:

Point point = SwingUtilities.convertPoint(
            getTable(), //the table as reference
            rectangle.getLocation(), //the rectangle for the table column header
            frame.getGlassPane()); //the containing JFrame
Uli
  • 1,390
  • 1
  • 14
  • 28
1

I figured it out. It was a combination of 2 issues. First, thanks to Uli for his answer. Passing in the table is the correct move. Second, I was calculating the rectangle when the table was not visible on the screen. This produced incorrect coordinates.

I switched it to get the Rectangle once the table is shown, and this is correct.

user489041
  • 27,916
  • 55
  • 135
  • 204
0

im not Java espert at all, but just yesterday I was reading the Oracle Documentation and apparently there are methods that take care of this already (getToolTipText(MouseEvent)). Also they propose an alternate method to display help on the headers of a JTable based on the content. I recommend you take a look at that:

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