• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieving components inserted into a JTextPane

 
Richard Robbins
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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??
...saj
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...



HTH,
-Nate
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic