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));
Related
I'm applying an image to a JLabel using the command .setIcon() and then adding the label to a panel
public browser() throws IOException {
JLabel lblimg;
Image img;
ImageIcon ico;
img = ImageIO.read(new File("<FilePath>"));
ico = new ImageIcon(img);
lblimg.setIcon(ico);
lblimg.setBounds(300,90,120,120);
add(lblimg);
}
but this doesn't resize the image inside the label, this way just a slice of the image will appear if it is bigger than the label size.
Does anyone knows a method to insert an image to a label background, resizing the image into it?
Create a BufferedImage and get scaled instance, set its width and height to the width and height to that of the label. Now if you even resize the label, the image will cover the label.
For Example: lblimg.setBounds(300, 90, 300, 120);
BufferedImage bimg =ImageIO.read(new File("file path"));
ico = new ImageIcon(bimg.getScaledInstance(lblimg.getWidth(), lblimg.getHeight(), Image.SCALE_SMOOTH));
lblimg.setIcon(ico);
The following image has been scaled according to the label's width and height.
lblimg.setBounds(300, 90, 100, 50);
Here's a method I wrote some years back to resize an image in Java.
public ImageIcon picturePrep(ImageIcon icon) {
final int DESIRED_WIDTH = 880;
double imageWidth = icon.getIconWidth();
int imageHeight = icon.getIconHeight();
imageWidth = DESIRED_WIDTH/imageWidth;
imageHeight = (int) (imageWidth * imageHeight);
Image img = icon.getImage();
return new ImageIcon(img.getScaledInstance(DESIRED_WIDTH, imageHeight, Image.SCALE_SMOOTH));
}
Use a appropriate layout manager to take care it for you automatically. See Laying Out Components Within a Container for more details.
If, for some bizarre reason, you can't use a layout manager, then you should probably be considering a solution based around custom painting, you could make use of the components preferred size to provide it better information when settings it's bounds
BufferedImage img = ImageIO.read(new File("<FilePath>"));
JLabel lblimg = new JLabel(new ImageIcon(img));
lblimg.setBounds(new Rectangle(new Point(300, 90), lblimg.getPreferredSize()));
Then, you don't need to make guesses
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>");
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);
I have images i want to add in the following folder: G:\my java\DesktopApplication1\build\classes\desktopapplication1\resources.
How to go about adding image in this folder to my labels or frames?
Once built, the image will typically be inside a Jar. The resources in a Jar can be accessed a number of ways, here is one.
URL urlToImage = this.getClass().getResource(
"/desktopapplication1/resources/the.png");
ImageIcon imageIcon = new ImageIcon(urlToImage);
JLabel ...
You can do something like this:
ImageIcon image = new ImageIcon("path_to_image");
JLabel label = new JLabel(image);
I did something like this:
JLabel l2=new JLabel("");
try {
Image img = ImageIO.read(getClass().getResource("resources/wait20.gif"));
l2.setIcon(new ImageIcon(img));
}
catch (IOException ex) {
}
It does work, but i would have liked it more had the GIF animation was displayed. Nevertheless, if a static image is to be displayed, this is the way to do it.
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.