Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Click on picture inside JEditorPane

 
Diederick de Vries
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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!
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • 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.
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Rob, you're right, that worked. Thanks a lot!
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic