0

I dont know why I cant add image to my JPanel i use code:

class PanelGlowny extends JPanel {

    private JLabel adam;
    private JButton henryk;

    PanelGlowny(){

        this.setLayout(new BorderLayout());
        ImageIcon imageurl = new ImageIcon("logo.jpg");
        //Image img = imageurl.getImage();
        adam = new JLabel(imageurl);
        add(adam, BorderLayout.NORTH);
        henryk = new JButton();
        add(henryk, BorderLayout.CENTER);

    }
}

Image is in the same folder as class, but if I use url to image it also do not add anything. This code adding button, but do not add image :(

The problem is probably with my JDE, or Sandbox or sth like this, because code should be fine.

user1304098
  • 71
  • 1
  • 3
  • 10
  • Works for me. Are you sure you have that image in your classpath. And if it is not working , is it throwing an exception ? – mbaydar Apr 13 '12 at 18:11
  • Yes, I'm sure, and as i told i tried also with URL. – user1304098 Apr 13 '12 at 18:14
  • This is a duplicate of your previous question: http://stackoverflow.com/questions/9997578/java-swing-how-to-add-image-in-north-of-jpanel (see my answer there which also answers this question) – ulmangt Apr 13 '12 at 18:15
  • ulmangt nope. In my previous question i had problem with code. Now the problem is probably with JDE, Sandbox or sth like this, because this code work on anodher computers. – user1304098 Apr 13 '12 at 18:18
  • If you provide an absolute path to the image does the code work? – ulmangt Apr 13 '12 at 18:23
  • *"probably with JDE"* I've never heard the abbreviation JDE. DYM JRE, IDE..? – Andrew Thompson Apr 13 '12 at 19:07

1 Answers1

1

Try this:

imageurl = new ImageIcon(getClass().getResource("logo.jpg"));

Check How to Use Icons tutorial.

EDIT: loading remote image

Try that to load your image from web:

public static void main(String args[]) {
    try {
        JOptionPane.showMessageDialog(null, "", "", 
            JOptionPane.INFORMATION_MESSAGE, 
            new ImageIcon(new URL("http://marinerczarter.pl/wp-content/themes/twentyten/images/headers/path.jpg")));
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
    } 
}
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • It doesnt work. I think the problem is isn't with code, but with my JDE, Java Sandbox or something. But i am updated it to newest version and it still doesnt work. – user1304098 Apr 13 '12 at 18:16
  • What does not, do you get an exception? Is image in same package with your class? If not, use relative paths in `getResource()` and start with `/` – tenorsax Apr 13 '12 at 18:24
  • It is, and i also use paths. If i put C:\\mariner.jpg, or http://marinerczarter.pl/wp-content/themes/twentyten/images/headers/path.jpg it also doesnt work. – user1304098 Apr 13 '12 at 18:41
  • @user1304098 , check if you see the image loaded from your url. – tenorsax Apr 13 '12 at 18:54
  • and when you run above `main()` that opens your image in `JOptionPane`? – tenorsax Apr 13 '12 at 19:36