Hello everybody,

I have some problems with my school assignment. I need to display a picture in a jlabel and i have already some code. Can someone give me some advice how i could get it to work.
Thanks in advance.

Greetz MeandJava

import java.awt.*;
import java.io.*;
import javax.swing.*;

public class LoadAnImage {

    public ImageIcon ImageIcon(String filename){
        filename = "";
        ImageIcon icon = new ImageIcon(filename);
        return icon;
    }

    public Image getImage(String path){
        Image image = null;

        // TODO OOP_PO2_A1: Insert code here
        return image;
    }

    public static void main(String[] args) throws IOException {

         LoadAnImage app = new LoadAnImage();
        Image image = app.getImage("chicks.jpg"); // TODO OOP_PO2_A2: Fill in the path to an existing image
        JLabel label = new JLabel(new ImageIcon(image));
        
        JFrame gallery = new JFrame();
        gallery.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gallery.getContentPane().add(label);
        gallery.pack();
        gallery.setLocation(200,200);
        gallery.setVisible(true);

        if(image != null && image instanceof Image){
            System.out.println("Successfully loaded the image file.");
        }
        else{
            System.out.println("Nothing loaded. Try again.");
        }
    }
}

try this out

Icon icon = new ImageIcon(file path);
JLabel label = new JLabel;
label.setIcon(icon);

First of all, in the ImageIcon method you pass the filename as argument and then you set it to "" . Why? Just pass the argument to the constructor and create the image. Why do you "delete" the argument by doing this:
filename = "";

And since you have the ImageIcon method to get the image you don't need the getImage method. You can keep it if you want though. The rest of the code seems ok.

You need to look at the API of the classes that you use. More specifically the constructor of the JLabel. If it does takes as argument an ImageIcon then look at the API of ImageIcon on how to create such object.

Post some new code with new question afterwards

I have read all your advices and hopefully I understand what you have said. So here's my new code:

The problem is i don't get a picture. I don't have errors i get a very small jframe but no picture. I hope someone could get me further. First i wanna thank you guys for helping me :)

/*
 * Let's load some pictures :)
 */

package gallery_test;

import java.awt.*;
import java.io.*;
import javax.swing.*;

public class LoadAnImage {

    public ImageIcon ImageIcon(String filename){
        ImageIcon icon = new ImageIcon(filename);
        return icon;
    }

    

    public static void main(String[] args) throws IOException {

        // LoadAnImage app = new LoadAnImage();
        LoadAnImage icon = new LoadAnImage();

        
        ImageIcon image = icon.ImageIcon("chicks.jpg"); // TODO OOP_PO2_A2: Fill in the path to an existing image
        //JLabel label = new JLabel(new ImageIcon(image));
        JLabel label = new JLabel();
        label.setIcon(image);
        
        JFrame gallery = new JFrame();
        gallery.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gallery.getContentPane().add(label);
        gallery.pack();
        gallery.setLocation(500,450);
        gallery.setVisible(true);

        if(image != null && image instanceof ImageIcon){
            System.out.println("Successfully loaded the image file.");
        }
        else{
            System.out.println("Nothing loaded. Try again.");
        }
    }
}

Does the "Successfully loaded the image file." gets printed?
I will run your code in a few minutes, but you can also try to use the full path of the image: Image image = app.getImage("C:/.../.../chicks.jpg");

Yes, successfully loaded the image file is printed:

run:
Successfully loaded the image file.
BUILD SUCCESSFUL (total time: 5 seconds)

Greetz MeandJava

Thanks man i got it solved, I had to give the full path of the image now it's working. thanks for your advice.

The only problem is that even the image does not exist. He still prints successfully loaded the image file

Because when you do this: image instanceof ImageIcon You only check if the java object is an ImageIcon. It will always be true because you create an ImageIcon instance. You can try to create a File and check if it exists:

File file = new File("......")
if (file.exists()) {
  JLabel label = new JLabel(new ImageIcon(image));
  // and the rest of the code you have in the main for displaying the JFrame
} else {
  // the error message you print
  System.out.println("Nothing loaded. Try again.");
}

Or you can look at API of ImageIcon for more information.

Ok i have searched on the things you talked about and this is what I came up with:

//Check if file exists or not.
        File file = new File(image); //THIS DOESNT WORK BECAUSE HE CANT FIND SYMBOL CONSTRUCTOR FILE.
        boolean exists = file.exists();
    if (!exists) {
      // It returns false if File or directory does not exist
      System.out.println("the file or directory you are searching does not exist : " + exists);

    }else{
      // It returns true if File or directory exists
      System.out.println("the file or directory you are searching does exist : " + exists);

      JLabel label = new JLabel(); //Make new label where image will be on printed.
        label.setIcon(image);
        JFrame gallery = new JFrame();
        gallery.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gallery.getContentPane().add(label);
        gallery.pack();
        gallery.setLocation(500,450);
        gallery.setVisible(true);
      
    }

Check the constructor of the File API. It takes as argument a String. What do you think that String would be?

ok man i fixed it thank you very much for your help here is my final code:

/*
 * Let's load some pictures :)
 */

package gallery_test;

import java.awt.*;
import java.io.*;
import javax.swing.*;

public class LoadAnImage {

    public ImageIcon ImageIcon(String filename){
        ImageIcon icon = new ImageIcon(filename);
        return icon;
    }

    public static void main(String[] args) throws IOException {

        LoadAnImage icon = new LoadAnImage(); 
        ImageIcon image = icon.ImageIcon("C:/Users/Patrick/Pictures/24.jpg"); // Fill in the path to an existing image
        
        //Check if file exists or not.
        File file = new File(image.toString()); //THIS DOESNT WORK BECAUSE HE CANT FIND SYMBOL CONSTRUCTOR FILE.
        boolean exists = file.exists();
    if (!exists) {
      // It returns false if File or directory does not exist
      System.out.println("the file or directory you are searching does not exist : " + exists);

    }else{
      // It returns true if File or directory exists
      System.out.println("the file or directory you are searching does exist : " + exists);

      JLabel label = new JLabel(); //Make new label where image will be on printed.
        label.setIcon(image);
        JFrame gallery = new JFrame();
        gallery.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gallery.getContentPane().add(label);
        gallery.pack();
        gallery.setLocation(500,450);
        gallery.setVisible(true);
      
    }
  }
}

ok man i fixed it

ok i suggest you mark the thread solved

how to add button in dis program??? plz suggest me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.