1

I want to add image in NORTH part of my JPanel, but it doesnt work. What should i do?

class PanelGlowny extends JPanel 
{    
    PanelGlowny()
    {        
        this.setLayout(new BorderLayout());
        ImageIcon imageurl = new ImageIcon("logo.jpg");
        Image img = imageurl.getImage();
        this.add(img, BorderLayout.NORTH);

    }
}

public class Formatka extends JFrame 
{    
    private PanelGlowny panel = new PanelGlowny();

    public Formatka()
    {    
        ...
        add(panel);
    } 
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
user1304098
  • 71
  • 1
  • 3
  • 10
  • 2
    What exactly is your directory structure ? Where are you keeping your images ? Do access your image by using `getClass().getResource("/logo.jpg")`, that's how you access `Application Resources`. Both the answers are too good :-) Are you using some IDE for making Swing Applications ? Hope this [Link](http://stackoverflow.com/a/9866659/1057230) can help you more. – nIcE cOw Apr 03 '12 at 17:05

2 Answers2

4

Here's a working modification of your code. You should be able to run it as is. Essentially, you can't simply add the ImageIcon to the JPanel. You need to wrap it in a JLabel first.

import java.awt.BorderLayout;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test2
{
    public static class PanelGlowny extends JPanel
    {

        public PanelGlowny( )
        {

            this.setLayout( new BorderLayout( ) );

            // incorporated @nIcE cOw's comment about loading classpath resources
            URL url = getClass().getResource("logo.jpg")
            ImageIcon imageicon = new ImageIcon( url );
            JLabel label = new JLabel( imageicon );

            this.add( label, BorderLayout.NORTH );

        }
    }

    public static void main( String[] args )
    {
        JFrame frame = new JFrame( );

        frame.add( new PanelGlowny( ) );

        frame.setSize( 400, 400 );

        frame.setVisible( true );
    }

}
ulmangt
  • 5,343
  • 3
  • 23
  • 36
  • As @nIcE cOw noted, the problem is most likely that your `"logo.jpg"` image is not being found. Try providing a **absolute** path to your image as a first test. That should definitely work. Then use `getClass().getResource("logo.jpg")` like @nIcE cOw mentioned to get a `URL` which you can pass into the `ImageIcon` constructor. – ulmangt Apr 03 '12 at 19:42
  • Also, when you use `getClass().getResource("logo.jpg")` you need to make sure that the `logo.jpg` image is somewhere on your [classpath](http://en.wikipedia.org/wiki/Classpath_(Java)) – ulmangt Apr 03 '12 at 19:43
2

Create a new JLabel from your ImageIcon and add that to your JPanel.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • I added it private JLabel adam; ' PanelGlowny(){ this.setLayout(new BorderLayout()); ImageIcon imageurl = new ImageIcon("logo.jpg"); //Image img = imageurl.getImage(); adam = new JLabel(imageurl); this.add(adam, BorderLayout.NORTH); }' But it doesnt show any image. – user1304098 Apr 03 '12 at 16:44
  • 2
    @user1304098: please don't try to post code in a comment as it looses all formatting. Instead edit your original question and post your code there, then place a comment here letting us know you've done this. Jeffrey's answer is correct (1+), so if your attempt to use it fails, you've got a bug and will need to show the well formatted code above in your original question. – Hovercraft Full Of Eels Apr 03 '12 at 16:53