Friday, July 6, 2012

Displaying Local Image in a JTextPane with HTML

Introduction

This is a question from a member in programmingforums.org. I have posted a quick reply there. I am going to write a step by step guide here.


Preparing a image

To display a image stored in local drive, you should first prepare a image file. We would expect "a.png" to be stored in the same folder as the demonstration program. It is easy to download or create a PNG image. For your convenience, I have prepared one here. Right click the image below and save it as "a.png"


Adding HTML in JTextPane

The following code would display a remote image in a JTextPane


Obtaining a local URL

Displaying remote image is not always desirable. To display a local image, we need a URL pointing to the local hard disk. If you place "a.png" at the same folder as the main program, you would obtain the image URL with the following portable way.


Full source

No comments:

Post a Comment