How set image icon to JButton

Aleksei Bulgak picture Aleksei Bulgak · Feb 18, 2013 · Viewed 25.7k times · Source

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));

Answer

Guillaume Polet picture Guillaume Polet · Feb 18, 2013

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.