I want to give the user the facility to copy and paste Images
in JTextPane
. Please help me.
Asked
Active
Viewed 1.2k times
4

Suhail Ahamed
- 136
- 1
- 2
- 7
-
4dude, What have you done for this until now? – Rahul Borkar Apr 03 '12 at 12:42
-
1I googled "image jtextpane", and the top 5 results all contained a solution with code on how to do this. So my suggestion is, try implementing one of those examples, and if you get stuck, post that specific question here and we can try to help you out – John Snow Apr 03 '12 at 12:47
-
@JimmyGustafsson Any links at SO you'd care to share? A Good SO link with an accepted answer would be ample grounds to close as a duplicate. – Andrew Thompson Apr 03 '12 at 12:49
-
I am Sorry, I was not online for the past 3 hours and above. I will try it now. Thank you very Much!!!! – Suhail Ahamed Apr 03 '12 at 15:55
-
I googled and found one. It displays but I want the user to Copy the Image and paste it in the TextPane, like WordPad and MS Word. – Suhail Ahamed Apr 03 '12 at 16:11
2 Answers
5
There is an easy way to add an image:
JTextPane pane = new JTextPane ();
pane.insertIcon ( new ImageIcon ( "/path/to/image.png" ) );
But there is no simple way to copy an image from the pane, since it cannot be selected and cannot be easily located in the pane document.

Mikle Garin
- 10,083
- 37
- 59
-
For my own uses I found that this works even better: `pane.insertComponent( new JLabel(myIcon) )` – JFreeman Nov 25 '18 at 07:44
4
You need a StyledDocument, like resulting from HTML. Hence set the content type to "text/html". Then <img src="file:...">
will link to an image on the file system.
Then you can provide drag-and-drop or paste from File or Image (the latter you have to save in your own files).

Joop Eggen
- 107,315
- 7
- 83
- 138