How to add images in JTextPane?

你说的曾经没有我的故事 提交于 2019-12-12 11:25:32

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/9993424/how-to-add-images-in-jtextpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!