2

I've created GUI for my application using Netbeans' GUI Builder. I am trying to display a JFrame containing a JLabel with an image, and I can't get the Image to display.

My generated code :

private void initComponents() {
        //...
        jLabel1 = new JLabel(new ImageIcon(myPicture));
}

And my class code:

public class GUIWindow extends javax.swing.JFrame {

    BufferedImage myPicture;

    /** Creates new form GUIWindow */
    public GUIWindow() throws IOException {
        myPicture = ImageIO.read(new File("images/logo.png"));
        initComponents();
        this.add(jLabel1);

    }
}

but I still don't see an image ... (path to the image file is fine) its sth like:

my-project :
  /build
  /dist
  /images/logo.png
  /nbproject
  /src (here I have all my source files)
  /build.xml
  /manifest.mf
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Brian Brown
  • 3,873
  • 16
  • 48
  • 79
  • 1
    Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230) and this [example](http://stackoverflow.com/a/11372350/1057230) – nIcE cOw Jul 30 '12 at 15:54

5 Answers5

6

you can use like this

URL imgSmartURL = this.getClass().getResource("your image path");
jLabel1 = new JLabel(new ImageIcon(imgSmartURL), JLabel.CENTER);
Shantanu Banerjee
  • 1,417
  • 6
  • 31
  • 51
  • I dont think the OP needs that, his folder is located in his projects directory not in the jar itself – David Kroukamp Jul 30 '12 at 15:48
  • @DavidKroukamp: Ok, I see... I tested it but I still don't see the image – Brian Brown Jul 30 '12 at 15:55
  • +1, for using getClass().getResource(...), the nice way to access Application Resources. – nIcE cOw Jul 30 '12 at 15:59
  • @DavidKroukamp Unless I am mistaken, the `logo.png` will become an embedded resource by the time this app. is deployed. Then it will be easiest to add it into the Jar and use `getResource(String)`. If it needs to be changed, might as well change it now. – Andrew Thompson Jul 30 '12 at 23:51
2

I would do something like this instead.

    JLabel dice1 = new JLabel();
    ImageIcon one = new ImageIcon("dice/1.png");

    //set dice1 position
    dice1.setLocation(20, 100);
    dice1.setSize(115, 115);
    dice1.setIcon(one);
    gamepanel.add(dice1);
Max McKinney
  • 1,218
  • 15
  • 25
1

If you are using netbeans you can directly add an image to a jLabel by setting properties. Right click on the jLabel -> properties -> icon -> (if it's external image) import to project(upload your image) -> ok . It'l be added into your jLabel.

Luna
  • 956
  • 4
  • 15
  • 28
1
  1. I'd suggest you copy the image in a seperate folder(images).
  2. Then use Toolkit.getDefaultToolkit().getImage("images/A.png");

I believe there's a similar question

Community
  • 1
  • 1
Sorter
  • 9,704
  • 6
  • 64
  • 74
0

private ImageIcon imageIconPrint =

new ImageIcon(getClass().getResource("/image/print.gif"));

create button and add follwing code:

jbtCanada.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

    jlblFlag.setIcon(imageIconCanada);

  }

});

this would help i think