|
| |||||||||||||||
|
|
|
|
|
Java Tips -- Page 1 |
|
Embed an image in a Swing componentThis was brought to my attention recently, in a couple of threads that
I replied to on Experts Exchange.
...
JLabel lbl = new JLabel( "<html><body><img src=\"http://server/image.jpg\"></body></html>" ); yourJFrame.getContentPane().add( lbl ); ... Download the source. |
|
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.
...
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) Download the source. |
|
|
Go to next page. |
|
|
Home | Profile | Hosting
| Info | News | Fun Stuff | Tech | Support
| Contact Us
| J2ME tips and news
© Copyright liviutudor.com.
| |||||||||||||||
| |
|||||||||||||||