1

I was wondering if any of you knows a good method to get the actual JComponent instance rendering a column header in an existing JTable.

Could not find anything helpful on google, so I came here. :)

I already know that there is a TableCellRenderer interface which delivers the component I want to get, but there is no guarantee that it'll return the same instance every time, so it is more or less useless to me.

Edit:

The goal is to have a possibility to get the Location and size of the component representing a column header dynamically.

And by column header component I mean the instance of the following object:

enter image description here

I really can't post any useful code example, sorry.
Actually any JTable example will do, as the problem refers to the standard Java JTable functionality.

Sam
  • 7,252
  • 16
  • 46
  • 65
pmkrefeld
  • 185
  • 3
  • 14
  • 1
    What's your actual problem? What do you want achieve? post [MCVE](http://stackoverflow.com/help/mcve) for help. – alex2410 Apr 04 '14 at 13:22
  • *The goal is to have a possibility to get the Location and size of the component representing a column header dynamically.* Ok, but why? What do you want/need to do with this component? – dic19 Apr 04 '14 at 13:51
  • I need those values (Location, Size) for the ability to create a component which will imitate those two properties. Those components then have no size or location, instead they delegate any calls to the components they are imitating. I need for some masking features on our GUI. – pmkrefeld Apr 04 '14 at 13:57
  • Here's one way to [*put a control in the* `JTableHeader` *of a* `JTable`](http://stackoverflow.com/q/7137786/230513). – trashgod Apr 04 '14 at 17:21

2 Answers2

3
Rectangle r = table.getTableHeader().getHeaderRect(...);
camickr
  • 321,443
  • 19
  • 166
  • 288
1

Is table.getColumnModel().getColumn(i).getHeaderRenderer() what you are looking for?

Ordous
  • 3,844
  • 15
  • 25
  • Unfortunately, getHeaderRenderer() returns on an Object implementing the TableCellRenderer interface, it is not a Component instance itself (at least not necessarily). Also according to Javadoc this method will return null if default header renderer is used. – pmkrefeld Apr 04 '14 at 14:12
  • Indeed, in that case the `table.getHeader().getDefaultRenderer()` is used. If you want to use some attributes from the component, then you might find `table.getTableHeader().getAccessibleContext().getAccessibleChild(i)` useful. But I don't think anything stops the JTable to create a new component on each render if a non-default renderer is used. – Ordous Apr 04 '14 at 14:30
  • As it seems there is no clean way to get that properties. I'll have to live with that :) Anyway thanks to you all for your effort. – pmkrefeld Apr 04 '14 at 14:53