4

I'm not very good in creating Swing application. So I have question how to set icon to JButton.

My project structure looks like this:

enter image description here

and I have simple JButton in MainWindow class: it looks like this:

tactButton = new JButton("next tact");

and I want to set image to this button using method setIcon. My code looks like this:

tactButton.setIcon(new ImageIcon(getClass().getResource("/images/button_next.jpg")));

but when I start app I have exception:

java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at by.bulgak.conveyor.gui.MainWindow.<init>(MainWindow.java:117)
    at by.bulgak.conveyor.gui.MainWindow$1.run(MainWindow.java:46)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

So I tried different things:

  • put all pictures in the same folder as MainWindow class
  • put pictures in project root folder
  • trying something like tactButton.setIcon(new ImageIcon("/images/button_next.jpg"));

but I have this exception or if I use tactButton.setIcon(new ImageIcon("/images/button_next.jpg")); I have simple button without image.

Finally I wrote absolute path to my image and this works fine (but absolute path is not good idea). So can you help me please?

I looked at the question How do I add an image to a JButton and tried to do it like there.

UPDATE

Full code of creating button and set icon:

tactButton = new JButton("next tact");
tactButton.setSize(100, 100);
tactButton.setIcon(new ImageIcon(MainWindow.class.getResource("/images/button_next.jpg")));
tactButton.addActionListener(new ProcessorNextStepListener(this));
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Aliaksei Bulhak
  • 6,078
  • 8
  • 45
  • 75

3 Answers3

4

If you are using Maven, then you should not need to do anything. M2E will take care of everything for you.

Put your source code in src/main/java

Put your resources (images, text files, etc...) in src/main/resources

If you don't use the default folders, then you should modify the pom.xml file to indicate where your sources and resources are located. After that, right-click on your project, go to Maven-->Update project... and make sure that you check the box "update project configuration from pom.xml"

That's it. You should be good to go.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
3

Try

new ImageIcon(MainWindow.class.getResource("/images/button_next.jpg");

Also, try to convert this .jpg to .png :D

Gabriel Câmara
  • 1,249
  • 15
  • 22
  • the same NullPointerException :( – Aliaksei Bulhak Feb 18 '13 at 13:01
  • Hm.. The JButton is being instantiated before the setIcon, right? When you double-click your "button_next.jpg" in the images folder, what happens? It opens with no problems? – Gabriel Câmara Feb 18 '13 at 13:09
  • Yes it opens without any problem. – Aliaksei Bulhak Feb 18 '13 at 13:12
  • 2
    [better than any of tutorial by @Gagandeep Bali](http://stackoverflow.com/questions/9864267/load-icon-image-exception/9866659#9866659) – mKorbel Feb 18 '13 at 13:21
  • Really wondering who up-voted this as it performs the exact same thing as the code in the question. Issue for this kind of stuffs is always the same, the image resource is not properly located in a directory of jar of the classpath. – Guillaume Polet Feb 18 '13 at 13:44
  • 1
    @AlekseiBulgak I saw that your using Maven, so you only need to work properly with Maven and M2E. Split your sources and resources and indicate them correctly in your pom.xml – Guillaume Polet Feb 18 '13 at 13:49
1

How are you running your application? Directly from Eclipse, or are you building a jar via Maven?

I see a pom.xml file in your tree, so I suspect that you are building via Maven. In that case, Maven is probably not putting any of the resource files (i.e. anything but .java files) in your output jar. Maven expects source files to go in a different directory from resources. By default, these directories are src/main/java and src/main/resources. However, it looks like you may have customized the source directory for your project, since I see your source files directly in the src directory.

I would inspect your resulting .jar file to ensure that the images actually ended up in there.

Eric Galluzzo
  • 3,191
  • 1
  • 20
  • 20