1

I have a website, and a JEditorPane set up already. I'd like to iterate through all the images on the website, and add them to the editor pane, like so:

for (Image image : website.getImages()) {
    pane.add(image);
}

Obviously, this is not the real code, but I'd like something like that. Is this possible? I only want to add the images from the website, and no other non-essential HTML code/text.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • @AndrewThompson yes, i do, but the problem is that the webpage is regenerated, and has new images each time, so i need to iterate through the images rather than hard code in each one – primevibetime Oct 23 '21 at 18:55
  • You can traverse the document like [this](https://stackoverflow.com/a/9561545/230513) to search for the `IMG` tag. – trashgod Oct 23 '21 at 18:56
  • [Extract url from Java String containing tag](https://stackoverflow.com/questions/17908302/extract-url-from-java-string-containing-img-src-tag) - It's then your choice about how you want to display them – MadProgrammer Oct 23 '21 at 21:52
  • You can also make use of jsoup for [example](https://stackoverflow.com/questions/3391422/jsoup-image-tag-extraction) – MadProgrammer Oct 23 '21 at 21:53

1 Answers1

1

A JEditorPane can render HTML, and images can be loaded within that HTML. So the easiest way to display images is using HTML to embed them.

Here is an example that does exactly that. This HTML loads and displays two images. Put that HTML in an editor pane configured to display HTML, and the images should appear.

<html>
<body>
<img src='https://i.stack.imgur.com/gJmeJ.png'>
<img src='https://i.stack.imgur.com/xj49g.png'>
</body>
</html>
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433