0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
i262666
  • 23
  • 5
  • *"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 Answers2

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:

  1. Element to string in HTMLDocument

  2. How to get current html element(tag) in JTextPane?

Community
  • 1
  • 1
Tahseen Adit
  • 184
  • 2
  • 7
  • 22
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