-1

I am trying to add a picture of a cow and eclipse is failing me. It won't allow any images, I think I may be doing my path wrong, but I am getting it straight from the properties that eclipse supplies upon right-clicking the image. So if anyone has any idea why my picture fails to load please tell me. NO ERROR MESSAGE!

package odin;

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

public class Main extends JFrame implements ActionListener{
    JPanel mypanel;
    JButton mybutton;
    JLabel mylabel;
    int Counter = 0;

    public Main(){
        mypanel = new JPanel();

        mybutton = new JButton("OK");
        mybutton.addActionListener(this);

        mylabel = new JLabel();
        JLabel imgLabel = new JLabel(new ImageIcon("/GuiTest/src/odin/COW.png"));

        mypanel.add(mybutton);
        mypanel.add(mylabel);
        mypanel.add(imgLabel);
        this.add(mypanel);
    }

    public static void main(String[] args){

        Main first = new Main();
        first.setTitle("First Attempt");
        first.setSize(800,600);
        first.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        first.setVisible(true);
        first.setResizable(false);
        first.setLocationRelativeTo(null);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==mybutton)
        {
            Counter = Counter + 1;
            mylabel.setText("My Clicks " + Counter);
        }
    }
}
JamoBox
  • 764
  • 9
  • 23
  • As we asked in your last question, where is `/GuiTest/src/odin/COW.png` actually stored? – MadProgrammer Jan 01 '14 at 09:37
  • That seemed to fail as well. I changed it to put in Project > src > IMAGE. I now have JLabel imgLabel = new JLabel(new ImageIcon("/GuiTest/src/COW.png")); Still no success in image loading. – user3121121 Jan 01 '14 at 09:38

1 Answers1

4

The problem is the path to the image...

JLabel imgLabel = new JLabel(new ImageIcon("/GuiTest/src/odin/COW.png"));

Basically, ImageIcon(String) expects that the String represents a file. This means, that Java is look for the image starting from the root of the current drive...which probably isn't what you really want...

You should also not store resources within the src directory in Eclipse, as I understand it, Eclipse requires you to place these resources within the "resources" directory within the project. These will be included within the project when you build it...

Once you've move the image to this location, you should be able to access it as an embedded resource using something like...

JLabel imgLabel = new JLabel(new ImageIcon(getClass().getResource("/odin/COW.png")));
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you, my cow is now loaded. Thank you MadProgrammer, every tutorial I looked up on youtube didn't help. I also tried some other questions, but when I put them in my path was always incorrect. glad you came along to help me. One more question, how would I source the image if I were to put it into Project > RSRC FOLDER > cow. – user3121121 Jan 01 '14 at 09:44
  • Assuming that `cow` is directory, I believe it would be something like `/cow/COW.png` or `/resources/cow/COW.png`... – MadProgrammer Jan 01 '14 at 09:51
  • *"every tutorial I looked up on youtube didn't help"* That is what you get for relying on **videos** to learn coding. – Andrew Thompson Jan 01 '14 at 10:31