-1

Could you tell me how can I download image from Url (ex: from "http://www.google.ru/intl/en_com/images/logo_plain.png") to JLabel but without saving it on HDD?

Could you give me an easy example? All I need is to put the image from the URL to JLabel (with ImageIO for ex.)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Vlad Pavlov
  • 25
  • 2
  • 5
  • 1
    this can be useful :) > [http://stackoverflow.com/questions/13188874/getting-jlabel-image-url-in-java][1] [1]: http://stackoverflow.com/questions/13188874/getting-jlabel-image-url-in-java – DRastislav Dec 17 '12 at 19:22
  • 1
    [http://stackoverflow.com/questions/13188874/getting-jlabel-image-url-in-java][1] [1]: http://stackoverflow.com/questions/13188874/getting-jlabel-image-url-in-java – DRastislav Dec 17 '12 at 19:24

2 Answers2

2

Something like

JLabel label = new JLabel(new ImageIcon(ImageIO.read(new URL("http://www.google.ru/intl/en_com/images/logo_plain.png")));

This going to throw a number of exceptions, so beware

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • FWIW: No need to go through `ImageIO` (although it is not incorrect), you can use the `URL` directly in the `ImageIcon` constructor – Guillaume Polet Dec 17 '12 at 20:18
  • @VladPavlov ImageIcon has the benefit of using a background thread for reading the aimed, this means that it won't block the EDT while its downloading/loading the image. ImageIO will return only after the image has finished downloading. Which is best will depend on your needs – MadProgrammer Dec 17 '12 at 20:48
  • 2
    @GuillaumePolet One aspect of `ImageIcon` that I dislike is that it will fail silently if handed an `URL` that does not point to an image it supports. (Though I have to admit 'image loading' becomes a complex & arcane subject in some corner cases e.g. don't use `ImageIO` for an animated GIF such as http://1point1c.org/gif/thum/plnttm.gif - you end up with first frame only..) – Andrew Thompson Dec 17 '12 at 22:53
2

Since you don't seem to require using ImageIO even though you mention it few times, this is the simplest way I know:

label = new JLabel(new ImageIcon(new URL(urlString)));
msell
  • 2,168
  • 13
  • 30