The application I am writing inserts JLabels into a JTextPane using the insertComponent method. I need to be able to retrieve the JLabel at any given position within the JTextPane. Does anyone know how to accomplish this?
Solving this was really a pain... JTextPane has Container as an ancestor, so using getComponentAt( x, y ) was a pretty obvious choice to use... However, it kept returning some stupid inner class of javax.swing.text.ComponentView as the component insteand of the JLabel... This inner class was not documented in the APIs, so I used reflection on it to figure out which methods it had. Somewhere down the line it also inherited from container, so I tried getComponents()... sure enough, I was able to pull the JLabel out of it!
And Sajee, it looks like you can solve your problem by adding the ImageIcon to a JLabel and then using insertComponent() instead of adding the Icon directly to the JTextPane!
Anyway, here's the code I finally ended up with that was able to pull JLabels out of a JTextPane... it's not great, but hopefully will illustrate what you need to do...
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
subject: Retrieving components inserted into a JTextPane