Sami
Sami

Reputation: 19

How to show both text and image from the web in jlabel

I'm trying to display both image from the web and text in JLabel only the image will show.

jlabel.setText("Hello" + "http://");

Upvotes: 0

Views: 64

Answers (2)

outlyer
outlyer

Reputation: 3943

JLabel has a setIcon method which you can use to set the image, pass it an ImageIcon, which can be created from an URL:

jlabel.setIcon(new ImageIcon(new URL("http:/...")));
jlabel.setText("Hello");

You can also include an image through HTML directly:

jlabel.setText("<html><img src=\"http://...\"></html>");

Upvotes: 0

Paco Abato
Paco Abato

Reputation: 4065

Try:

URL url = new URL("http://www.url.com/image.jpg");
Image image = ImageIO.read(url);
jlabel.setIcon(new ImageIcon(image));
jlabel.setText("the text");

Upvotes: 2

Related Questions