Aliaksei Bulhak
Aliaksei Bulhak

Reputation: 6208

How set image icon to JButton

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:

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

Upvotes: 4

Views: 27445

Answers (3)

Eric Galluzzo
Eric Galluzzo

Reputation: 3241

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.

Upvotes: 1

Guillaume Polet
Guillaume Polet

Reputation: 47637

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.

Upvotes: 4

Gabriel C&#226;mara
Gabriel C&#226;mara

Reputation: 1249

Try

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

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

Upvotes: 3

Related Questions