Say a plain text and an image are entered by a user into a text pane. How to separately read the text and the image from such text pane?
Asked
Active
Viewed 113 times
0
-
*"Say a plain text and an image are entered by a user into a Text Pane."* I'm pretty sure if the text pane can show an image, the text is no longer 'plain'. Show me different in a [mcve]. One way to get an image for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Aug 21 '16 at 05:03
-
follow this article >> [How can I load an image and write text to it using Java](http://stackoverflow.com/questions/6540376/how-can-i-load-an-image-and-write-text-to-it-using-java) – DASUN CHINTHAKA Aug 21 '16 at 08:56
2 Answers
0
If you have HTMLEditorKit set as the default editor kit, then get all the elements of the document. Then iterate through those elements and look for image element. Then separate them from the text elements. Following links may come to your help:

Community
- 1
- 1

Tahseen Adit
- 184
- 2
- 7
- 22
-
If this solved your problem, then please mark this as approved answer. Glad to help. – Tahseen Adit Aug 23 '16 at 04:49
0
To get the plain text from a JTextPane, you can use:
myJTextPane.getDocument().getText(...)
To get the image, you need to use StyleConstants.getIcon(...)
. For instance, to get the image at offset i:
StyleConstants.getIcon(((DefaultStyledDocument)myJTextPane.getDocument()).getCharacterElement(i).getAttributes()))

Sharcoux
- 5,546
- 7
- 45
- 78