• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Click on picture inside JEditorPane

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a JEditorPane which displays some HTML document with a picture, using an img tag with just a source parameter with something like file:///path/to/my/picture.jpg as its value.

Now when I click inside the JEditorPane with my right mouse button, a mouse listener picks this up, which in turn has a pop up menu pop up. But now I want the pop up menu to have some extra options when I click on the picture.

My question is therefore: is there some way for me to find out if either the picture has been clicked, or the text around it?

Thanks for any help!
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
don't trust me. you will probably get a better answer later. but unless the image is always in the same place, i don't think you can.
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Diederick,
I'm not sure if this is will resolve your problem, but are you familiar with method getDeepestComponentAt in class javax.swing.SwingUtilities ?

Good Luck,
Avi.
 
Sheriff
Posts: 22788
131
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think getDeepestComponent will help as the image most likely is not a separate component.

Your JEditorPane has a StyledDocument which should be able to return you the image only using getCharacterElement. Now all you need to do is translate the mouse click into a position. That sounds easier than it is though, as I haven't found a way to do this yet.

Edit: and I may have found it. JTextComponent has method viewToModel that can convert a Point to an offset.
 
Diederick de Vries
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, you're right, that worked. Thanks a lot!
 
Rob Spoor
Sheriff
Posts: 22788
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic