Search code examples
javaimageswingjlabelimageicon

Adding Image from Filesystem to Jlabel


I working on a project, where i need to display images in a JFrame. The images are downloaded dynamically and need to be stored outside the jar file. How do i display images (using Jlabel) from the computer's file-system ?


Solution

  • Suppose the images are stored in a folder say 'image' or whatever location. I am supposing that you have an image folder inside src (source folder), though you may change it to whatever location you want.

    /*
    URL logoUrl = this.getClass().getResource("/images/login_icon.png"); // you can change this location
    Toolkit tk1 = this.getToolkit();   
    logo = tk1.getImage(logoUrl);
    */
    // use above code if the image lies within your jar file
    // otherwise use below code for images stored in path like C:\User\Desktop ..
    
    ImageIcon image = new ImageIcon("C:\\Users\\Public\\Pictures\\Desert.jpg");    
    jLabel2.setIcon(image);  // where jLabel2 is your label
    

    This will work!