0
ImageIcon img = new ImageIcon(pathToFileOnDisk);
myFrame.setIconImage(img.getImage());

I can't seem to find a way to display a picture stored on the computer when a button is clicked. I am trying to do so by setting an icon to a label, through the code (not properties), as I want to be able to change the picture through the button being clicked again. I would like the picture stored in the project (if possible). I would like help on having a working way to have an image displayed in my JFrame as me trying things and searching on this website for the last 2 hoours didn't help. I would provide code to the button but I constantly delete it as I each method I try doesn't work. I don't know what I am missing in imports either. I have tried these imports:

import java.awt.Color;
import javax.swing.Icon;
import javax.swing.ImageIcon;

and my errors

ImageIcon img = new ImageIcon("C:\Users\(user)\Desktop\pic.png");
//has "illegal escape character" as error
        RescuedImage.setIconImage(img.getImage());
//"cannot find symbol"

I would like to see a working sample with the imports shown if no one wants the headache of this. All I want to be able to do is have a button show an image stored on the computer by using its file location.

  • 2
    *"Does this work?"* Yes. *"If so, why doesn't work for me?"* No idea, consider providing a runnable example which demonstrates your problem. *"Whats needed to be imported?" java.awt.* and javax.swing.* might be a good start – MadProgrammer Feb 22 '15 at 04:19
  • `Whats needed to be imported?` are you asking how to compile the code or execute the code? If the code compiles then you have the proper imports. If it doesn't compile then why don't you state that in your question? Be specific what the problem is. – camickr Feb 22 '15 at 04:21
  • sorry for not being specific, was trying to be breif/concise. I just want to know of an easy way of displaying image from button.. Doesn't necessarily need to be the way I am trying. – codingaddict Feb 22 '15 at 05:03
  • 1
    `"C:\Users\Connor\Desktop\pic.png"` should either be `"C:\\Users\\Connor\\Desktop\\pic.png"` or `"C:/Users/Connor/Desktop/pic.png"` – MadProgrammer Feb 22 '15 at 05:06
  • *"I would like to see a working sample with the imports shown if no one wants the headache of this. All I want to be able to do is have a button show an image stored on the computer by using its file location."* - [How to Use Labels](http://docs.oracle.com/javase/tutorial/uiswing/components/label.html) – MadProgrammer Feb 22 '15 at 05:06
  • Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. *To do that, select the text and click the `{}` button at the top of the message posting/editing form.* – Andrew Thompson Feb 22 '15 at 05:15
  • You may also want to read the JavaDocs on what `setIconImage` actually does... – MadProgrammer Feb 22 '15 at 06:02
  • @codingaddict: Please have a look at this [thread](http://stackoverflow.com/a/9866659/1057230). Hopefully, this be of some help :-) – nIcE cOw Feb 22 '15 at 08:33
  • 1
    looking at the http://docs.oracle.com/javase/tutorial/uiswing/components/label.html link provided by MadProgrammer, and thread link http://stackoverflow.com/a/9866659/1057230 provided by nIcE cOw. Thank you all – codingaddict Feb 22 '15 at 18:01

1 Answers1

2

To load a file location..

jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\(user)\\Desktop\\pic.png"));

To have the picture saved to .jar, java project, drag the image into the project and you should see it shown (in netbeans it's under source packages)

code to show it as logo for label:

String image = "(pictureName).jpg";
    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource(image)));