I'm trying to add an image to a JLabel to put on JFrame and after hours of researching it still only loads the JFrame with 2 buttons on the bottom. The Image in question is in the same directory as the class except a folder down (I've tried in the same folder to no avail). I'm quite new to coding so please bear with me.
This is most of the code except the button's action listeners (the class extends JFrame as well)
JPanel jp = new JPanel();
JPanel jb = new JPanel(new GridLayout(1, 2, 20, 20));
JLabel jl = new JLabel();
JButton end = new JButton("End");
JButton next = new JButton("Next");
jl.setIcon(new ImageIcon("notes/daWay.jpg"));
setSize(900, 700);
jp.add(jl, new FlowLayout());
add(jp);
jb.add(end);
jb.add(next);
add(jb, BorderLayout.PAGE_END);
setVisible(true);
I've tried: setting label bounds, checked if ImageIcon was null (it wasn't), making ImageIcon its own variable then adding the variable to the Jlabel, getContentPane for JFrame, using a BufferedImage and ImageIO with try and catch (with url and without), getClass, getClassLoader, getResource, imageUpdate on the label, with FlowLayout and without, jpegs and pngs, clearing the frame except for the one label, and many other things. Even extending the window while running the image doesn't appear, Please Help!
(also in the future I'd like to clear the label and add a new image, I'm not at that point yet cause I can't get past this error but if context helps with the solution then context)
ImageIcon
is probably a bad place to start, as it won't tell you when something goes wrong. A better place (to hep you diagnose the issue) would be to useImageIO.load
- see Reading/Loading an Image for more details – MadProgrammerjp.add(jl, new FlowLayout());
doesn't make sense. In this case, just usejp.add(jl);
– MadProgrammer