home
home profile hosting info news fun stuff Technical Stuff support contact us
JAVA PROGRAMMING TIPS

Ask an Expert - Visit my Virtual Office at Kasamba

Java Tips -- Page 1

Embed an image in a Swing component

This was brought to my attention recently, in a couple of threads that I replied to on Experts Exchange.
Suppose you have an image hosted on a website which you want to show in your Swing application. One way of doing it is to download the image locally, package it in your jar file and set it in a JLabel. This works fine if your image on the webserver never changes and/or the clients running your software don't have (or don't want to use) and Internet connection while running the program. However, if the image is say rotating on a daily basis, you cannot apply this pattern, as it will mean you will have to make a new release of your software every time the image changes -- e.g. daily/weekly/etc -- very very very unpractical (not to mention silly :D). You could go down the route explained in the next tip and download the file locally, then show it in the control and then delete the image or so. Or you can create an Image from an URL as shown further down. Or you could just take advantage of the fact that all the Swing controls support HTML text! So assuming you want to set the image in a JLabel to http://server/image.jpg then all you have to do is set the text for the JLabel to: <html><body><img src="http://server/image.jpg"></body></html>. Your code therefore would look something like this:

...
JLabel lbl = new JLabel( "<html><body><img src=\"http://server/image.jpg\"></body></html>" );
yourJFrame.getContentPane().add( lbl );
...

Download the source.
Read our download disclaimer.

Download an image locally (and set it in a Swing component)

This is again something that I replied to on Experts-Exchange: suppose you want to allow your user to "personalize" your Swing application -- so in other words you allow your user to specify paths to images for various visual components in your application. You could use the method described in the previous paragraph of course, but that means that every time your application will run your program will use the Internet to read the image and display it. While that is not a problem in itself, if the images are not changing on the website and if you have a lot of images to retrieve from the internet, it might increase your application startup time unnecessarily. Instead of keeping retrieving the images from the net, you could download them locally the first time the user specifies the image location and then use them from there on each startup.
In order to download the image locally, you can use the URLConnection class which allows you to open an InputStream and read from it. So if your image is at http://server/image.jpg you could open an InputStream from a HttpURLConnection and read the image bytes from that InputStream and save them to a local file, like this:

...
URL url = new URL( "http://server/image.jpg" );
HttpURLConnection hcon = (HttpURLConnection)url.openConnection();
InputStream is = hcon.getInputStream();
FileOutputStream fos = new FileOutputStream( "image.jpg" );
byte buffer[] = new byte[1024];
int nRead = -1;
while( (nRead = is.read(buffer)) > 0 )
 fos.write( buffer, 0, nRead );
fos.flush();
fos.close();
is.close();
hcon.disconnect();
...

(obviously you can adjust the buffer size and file paths/etc. in the above example to your program requirements)
Once saved locally, you can create an Image from the file and set it in your JLabel.

Download the source.
Read our download disclaimer.

Go to next page.
Go back to the Technical page.
Go back to the main page.

Home | Profile | Hosting | Info | News | Fun Stuff | Tech | Support | Contact Us | J2ME tips and news

© Copyright liviutudor.com.
Based on a template from TemplatesBox.com

Valid CSS! Valid XHTML 1.0 Transitional Get FireFox! Powered by Debian Linux Powered by Apache Web Server No Software Patents View My profile on LinkedIn

Liviu Tudor personal web page details hobbies also loads of technical tips and fun stuff for you to browse code to download free and applications Java J2ME mobile code free mobile Java J2ME applications to download and use on your mobile phone read technical Java J2ME articles details for Liviu Tudor Lipu Liv have a look at the technical tips provided for various technical categories fun stuff funny quotes ASCII art drawing ascii bitmaps fun knowledge base technical tips programming sysadmin windows Java java knowledge base