public JLabel SetImage(String path) throws IOException {
        ImageIcon myPicture = new ImageIcon(path);//ImageIO.read(new File(path));
        JLabel picLabel = new JLabel();
        picLabel.setBounds(0, 0, 2, 2);
        picLabel.setIcon(myPicture);
        pnlImage.setLayout(null);
        pnlImage.add(picLabel);
        pnlImage.repaint();
        pnlImage.revalidate();
        return picLabel;
    }

    try {
    String Ref = txtRef.getText();
    SetImage();
    JLabel label = new JLabel();
    label = SetImage("C:\\Users\\Documents");
    App\\ImagesDoc_001.jpg");
    pnlImage.setLayout(null);
    pnlImage.add(label);
    pnlImage.repaint();
    pnlImage.revalidate();
    } catch (IOException ex)

can anyone help me with this piece of code.. i am trying to load an image but it isnt loading

Have you imported your image into your project?

yes i did

Perhaps the

label = SetImage("C:\\Users\\Documents");
App\\ImagesDoc_001.jpg");

is causing problems for you? I would bet you probably want

label = SetImage("C:\\Users\\Documents\\App\\ImagesDoc_001.jpg");

fixed that one but still wont work :/

Can you post your current code?

Try debugging your code by creating a File object with the path to the image and printing out the File class's exists() method's value.

Member Avatar for hfx642

I assume that pnlImage is a JPanel, but you haven't defined it.

} catch (IOException ex)

before i say much, is this your complete code? if not please post the complete code, because this to me is wrong it should be:

try {
}catch (IOException ex) {
//exception handling here
}

which if this is not your complete source is fine, but remeber when you say you are having problems and post code thats like that we are not sure if its an error or you just cut your code at a random section?

I'd recommend:

try {
}catch (IOException ex) {
   ex.printStackTrace();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.