0

I am having difficulty creating the image icon. I want to package a jar and have the images available to be displayed in the gui. Both of the following throw null pointer exceptions. The path is a path directly to a package in my Eclipse project that contains the necessary images.

ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource(
   "/TicTacToe/src/edu/luc/tictactoe/gui/resources/images/TicTacToeOIcon.png")
   .getPath());

and

ImageIcon icon = new ImageIcon(getClass().getResource(
   "/TicTacToe/src/edu/luc/tictactoe/gui/resources/images/TicTacToeOIcon.png"));

I can't see to be able to access the appropriate package. Any suggestions?

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Matthew Kemnetz
  • 845
  • 1
  • 15
  • 28
  • `TicTacToeOIcon.png` Are you sure about that `O`? BTW 1) having a resource 8 levels deep seems to be asking for trouble (especially when the code is having trouble locating the resource). 2) That `src` part of the path sounds like an IDE enforced directory structure that should have vanished by build stage. 3) Forget the version that has `getPath()`. The constructor would presume the `String` represents a `File` rather than an `URL`. – Andrew Thompson Dec 05 '11 at 12:05

2 Answers2

1
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader()
    .getResourceAsStream("edu/luc/tictactoe/gui/resources/images/TicTacToeOIcon.png");
kleopatra
  • 51,061
  • 28
  • 99
  • 211
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • (polite cough) In which version of the J2SE did they add an [`ImageIcon` constructor](http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html#constructor_summary) that accepts an `InputStream`? – Andrew Thompson Dec 05 '11 at 12:17
  • It worked when I used getResource instead of getResourceAsStream – Matthew Kemnetz Dec 05 '11 at 12:46
  • Andrew, you are right. I just copied from another source:-) thank you for the note! – StanislavL Dec 05 '11 at 12:51
  • Note that I don't get informed unless you start a comment with @Andrew. I just jumped back into this thread to try and figure why the OP had started a [new question](http://stackoverflow.com/questions/8385588/using-an-image-panel-with-a-jar) along much the same lines. ;) – Andrew Thompson Dec 05 '11 at 13:24
  • @AndrewThompson I did start a new thread because the questions are different. I believe you also answered my second question. But nontheless the two are two distinct questions. They are similar but different. Is there a preferred way to ask what I asked? From my experience with SO if I ask both question in the same thread only on gets answered. I made two threads for two questions and the were answered perfectly by StanislavL and yourself. – Matthew Kemnetz Dec 05 '11 at 18:14
  • @MatthewKemnetz Agreed. It was when I went back to the other question that I noted the differences, and provided an answer rather than voting to close. ;) – Andrew Thompson Dec 05 '11 at 20:40
0

Try like this

JButton myButton =new JButton(“press me”);
myButton.setIcon(new ImageIcon(image));

EDIT:

Refer to this discussion to know loading the image from executable Jar files

  • This is not what I'm looking for. I know this already. The creation of the ImageIcon is the problem. How do I store the necessary pngs and access them with the code such that when I export the runnable jar I get the correct output? – Matthew Kemnetz Dec 05 '11 at 12:02
  • I have edited the title and tags of the question to make it more clear. – Andrew Thompson Dec 05 '11 at 12:15