Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add images in JTextPane?

I want to give the user the facility to copy and paste Images in JTextPane. Please help me.

like image 542
Suhail Ahamed Avatar asked Jan 17 '23 00:01

Suhail Ahamed


2 Answers

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.

like image 198
Mikle Garin Avatar answered Jan 29 '23 19:01

Mikle Garin


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).

like image 25
Joop Eggen Avatar answered Jan 29 '23 19:01

Joop Eggen