File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Retrieving components inserted into a JTextPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Retrieving components inserted into a JTextPane" Watch "Retrieving components inserted into a JTextPane" New topic

Retrieving components inserted into a JTextPane

Richard Robbins
Ranch Hand

Joined: Mar 07, 2001
Posts: 66
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?

Richard Robbins is one of the founders of, a resource for free website templates and Photoshop tutorials. He also consults for an online boutique selling baptism dresses and christening gowns.
Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
I am facing the same problem. I insert an icon into JTextPane using insertIcon() method. But how do i retrieve it.
I posted this question..but no reply.
Did u solve it??
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

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.
I agree. Here's the link:
subject: Retrieving components inserted into a JTextPane
It's not a secret anymore!