I want to load an image for each different shoe and I'm using JLabel to help me do this.
I put the image's path in the JLabel constructor but when the code runs, it doesn't display any image. Instead of an image, the image path is written.

appreciate any help and suggestion

package badmintonstore;

/**
 *
 * @author TaimooR
 */

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

/*
 * The class displays shoes
 */
public class Shoes extends JPanel
{
    //Class Fields
    private JLabel icon;
    private JLabel price;
    private JTextField qtyInput;
    private double priceS = 0.0;


    public Shoes (String showPrice,String path)
    {
            setLayout(new GridLayout(2,2));

            // Constructing shoes
            icon = new JLabel(path);
            price = new JLabel(showPrice);
            qtyInput = new JTextField(2);
            priceS = Double.valueOf(showPrice);
            add(icon);
            add(price);
            add(qtyInput);
    }
    /*
     * Method getShowPrice
     * Return the price of the shows
     */
    public double getShoePrice()
    {
        double finalShoePrice;
        double quantity = Double.valueOf(qtyInput.getText());
        finalShoePrice = quantity*priceS;
        return finalShoePrice;
    }
}
Member Avatar for ztini
icon = new JLabel(path);

For images with JLabel, you need to pass in an Icon. See public JLabel(Icon image) for more information.

So, basically something like this:

icon = new JLabel(new ImageIcon(getClass().getResource(path)));

Make sure you have the images or image folder in your build path.

@ztini I tried what you suggested but I get a NullPointerException. Here is my main class. thank you in advance

public BadmintonStore()
    {
        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Shoes shoe = new Shoes ("199.99","C:\\Users\\TaimooR\\Desktop\\iPhoneS\\fifa11.jpg");
        add(shoe,BorderLayout.CENTER);
        pack();
        setVisible(true);
    }
    public static void main(String[] args)
    {
        BadmintonStore Montreal = new BadmintonStore();
    }

}

Here is the Updated Shoe class

package badmintonstore;

/**
 *
 * @author TaimooR
 */

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

/*
 * The class displays shoes
 */
public class Shoes extends JPanel
{
    //Class Fields
    private JLabel icon;
    private JLabel price;
    private JTextField qtyInput;
    private double priceS = 0.0;


    public Shoes (String showPrice,String path)
    {
            setLayout(new GridLayout(2,2));

            // Constructing shoes
            icon = new JLabel(new ImageIcon(getClass().getResource(path)));
            price = new JLabel(showPrice);
            qtyInput = new JTextField(2);
            priceS = Double.valueOf(showPrice);
            add(icon);
            add(price);
            add(qtyInput);
    }
    /*
     * Method getShowPrice
     * Return the price of the shows
     */
    public double getShoePrice()
    {
        double finalShoePrice;
        double quantity = Double.valueOf(qtyInput.getText());
        finalShoePrice = quantity*priceS;
        return finalShoePrice;
    }
}
Member Avatar for ztini

The example I gave you is for relative file location. That is, your call should look something like this: new Shoes(199.99, "images/shoe.jpg");

This requires the 'images' folder to be here:
C:\....\src\badmintonstore\images\shoe.jpg

if you don't want to use relative locations, just do:
icon = new JLabel(new ImageIcon(path));

They can be a pain to figure out at first, but you will be thankful you figured out relative paths later.

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.