#Java GUI not loading images/ImageIcons help

8 messages · Page 1 of 1 (latest)

manic sierra
#

Hey, so im going through the basics of GUI. At first it was going great but right now Ive been trying to a) set the ImageIcon of the frame and also setting the ImageIcon of a label but with my current implementation none of the pictures load and I dont know why. I have included both the code and the file strucutre (ps. i have also set the Pictures package to be Resources Root Directory):

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

public class Main {
    JFrame frame;
    public static void main (String[] args) {
        JFrame frame = new JFrame("Wowzzzers");
        frame.setSize(420,420);
        frame.getContentPane().setBackground(Color.DARK_GRAY);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
        ImageIcon img = new ImageIcon("/Pictures/yippee.jpg");
        ImageIcon img2 = new ImageIcon("/Pictures/these-days-were-wild-v0-vfwnc6uu1e7e1.webp");
        frame.setIconImage(img.getImage());

        JLabel label = new JLabel();
        label.setText("Wassssupp");
        frame.add(label);
        label.setForeground(Color.CYAN);
        label.setIcon(img2);
        label.setVisible(true);

        //frame.pack();
    }
}
twilit summitBOT
#

This post has been reserved for your question.

Hey @manic sierra! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

devout bramble
#

place images in resources folder, then load them using getResource() or you can use relative path referencing "/Pictures " uses absolute referencing and tries to locate /Pictures from the root file system

manic sierra
#

could you type what you meant

devout bramble
#

like
ImageIcon img = new ImageIcon(Main.class.getResource("/Pictures/yippee.jpg"));

if Pictures is your resources root folder, do like this
ImageIcon img = new ImageIcon(Main.class.getResource("yippee.jpg"));