This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Click on picture inside JEditorPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Click on picture inside JEditorPane" Watch "Click on picture inside JEditorPane" New topic
Author

Click on picture inside JEditorPane

Diederick de Vries
Greenhorn

Joined: Jul 14, 2005
Posts: 12
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

Joined: Oct 21, 2000
Posts: 4340
    
    2

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.


SCJP
Visit my download page
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1121

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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Diederick de Vries
Greenhorn

Joined: Jul 14, 2005
Posts: 12
Rob, you're right, that worked. Thanks a lot!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Click on picture inside JEditorPane
 
Similar Threads
Drop down calendar
HTMLEditorKit and JEditorpane Sizing!!
Help JMenu is getting frozen and won't go away
Right click in text editor
Disable Right Click on Plugin....Help....Urgent...