Show Image from URL in JLabel - Java Swing

Java examples for Swing:JLabel

Description

Show Image from URL in JLabel

Demo Code


import java.awt.BorderLayout;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ShowImageFromUrl {

    public static void main(String[] args) {
        Image image = null;/* w  ww .  ja  v  a 2 s.c o m*/
        try {
            URL url = new URL("http://server .com/9.jpg");
            image = ImageIO.read(url);
        } 
        catch (IOException e) {
        }

// Use a label to display the image
        JFrame frame = new JFrame();

        JLabel lblimage = new JLabel(new ImageIcon(image));
        frame.getContentPane().add(lblimage, BorderLayout.CENTER);
        frame.setSize(300, 400);
        frame.setVisible(true);
        
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(lblimage);
// add more components here
        frame.add(mainPanel);
        frame.setVisible(true);
    }

}

Related Tutorials