Display Image using html in JLabel - java

I used the following way to convert an ImageIcon into a thumbnail :
ImageIcon im = new ImageIcon(url);
Image image = im.getImage().getScaledInstance(80,100,Image.SCALE_DEFAULT);
Now I'm having problems when I try to display the Image in JLabel using html. Before converting the ImageIcon into Image, the following code was working fine:
JLabel label = new JLabel("<html>" + im);
But now when I try to do the same using the Image, it doesn't work. Any idea of how to display the resulting Image using html in a JLabel?

What's wrong with:
JLabel label = new JLabel("<html><img height='" + height + "' width='" + width
+ "' src='" + url + "'></html>");
For example:
JLabel label = new JLabel("<html><img height='80' width='100' src='https://developer.cdn.mozilla.net/media/img/mdn-logo-sm.png' /></html>");
Note: Don't add px to the height or width values
Some documentation here:
http://docs.oracle.com/javase/7/docs/api/javax/swing/text/html/ImageView.html

You cannot display HTML code inside a JLabel. You are going to need to use an htmlPanel. You can set the perferred size of it later, here is what you need to add.
JPanel htmlPanel = new HtmlPanel("<html><body><img src="file path goes here"></body></html>");
Here is the complete code I whipped up.
JPanel htmlPanel = new HtmlPanel("<html><body><img src="images/file.png"></body></html>");
htmlPanel.setPreferredSize(new Dimension(TheWidthGoesHere, TheHeightGoesHere));
Then you can just add the htmlPanel to your frame like you would with any other component.
frame.add(htmlPanel);

Related

Make label with any image (gif and other)

What I want to do is:
Load an image that it is a gif, jpeg etc ... coming from a URL (but it is only the gif that I do not arrive but it must work with the others)
Put it in a JLabel in IconImage (although it suits me if it is not in the icon of the label as long as it is in the label)
Make it auto resize by taking all the space of the label but keeping its ratio
And finaly make this.add (label)
I already managed to do it with any type of image but the gifs was freeze to the first image
I searched for more solutions but could not find any that worked for me.
So, if someone could help me from the very beginning. I have the URLs and can share, if required.
Exemple of my code
//that work but images is not resize
JLabel label = new JLabel();
label.setIcon(new ImageIcon(new URL(image.getPath())));
this.add(label);
//images don't appear
URL url = new URL(image.getPath());
JLabel label = new JLabel();
java.awt.Image img = Toolkit.getDefaultToolkit().getImage(url);
img = this.scaleImage(img,Toolkit.getDefaultToolkit().getScreenSize().width/6); label.setIcon(new ImageIcon(img));
Try this, you may need to put it in a try-catch
BufferedImage image = ImageIO.read(new URL(url));
JLabel label = new JLabel(new ImageIcon(image));

Adding Image In JFrame : Java

If I Use This Way To Add image in panel
static ImageIcon bg = new ImageIcon("hangman1.png");
Then Where Should Be Mine hangman1.png should be so it display correctly??
Load your "hangman1.png" file as a resource and add it to JLabel:
JLabel label = new JLabel(new ImageIcon(getClass().getResource("/path/to/your/image/hangman1.png")));
Before of that, add it into some package in your Java project.

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://");
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");
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>");

Rendering Image into label

I have a label. I want to render image into it. But the following code would do anything.
CardLayout cl = (CardLayout) (mainPanel.getLayout());
cl.show(mainPanel, "newPersonaCard");
BufferedImage myPicture = ImageIO.read(new File("C:\\Desktop\\Documents\\Pictures\\always.jpg"));
ImageIcon icon = new ImageIcon(myPicture);
icon.getImage().flush();
I am using netbean designer.
You are right, in some cases there issue with repainting Icon in the JLabel, then you have to call,
myIcon.getImage().flush();
myLabel.setIcon(myIcon);
rest of methods is implemented in the Icon and JLabel correctly
.
.
File file = fileChooser.getSelectedFile();
JLabel label = new JLabel();
ImageIcon icon = new ImageIcon(file.getAbsolutePath());
label.setIcon(icon);
//add label to panel
fileChooser.showDialog(saveBtn2, null);
File file = fileChooser.getSelectedFile();
System.out.println("The path to file "+file.getAbsolutePath());
ImageIcon icon = new ImageIcon(file.getAbsolutePath());
pictureLbl.setIcon(icon);

Displaying jpg image on JPanel

How can I display jpg image which I stored in arraylist in JPanel?
Im not able to display the jpg files in the JPanel.
String[] pictureFile = {"A.jpg","B.jpg","C.jpg"};
List<String> picList1 = Arrays.asList(pictureFile);
Collections.shuffle(picList1);
ImageIcon icon = new ImageIcon("picList1.get(0)");
JLabel label1 = new JLabel();
label1.setIcon(icon);
JPanel panel = newJPanel;
panel.add(label);
You should not put the call to the array in quotes.
Instead, you should try the following:
ImageIcon icon = new ImageIcon(picList1.get(0));
The problem is in the line
ImageIcon icon = new ImageIcon("picList1.get(0)");
It's interpreting the string as a file name. You should just need to unquote the picList1.get(0) bit.

Categories

Resources