0

I'm getting familiar with NetBeans and doing my first tutorials with Java.

I'm not seeing my System Tray icon show up, and I suspect I placed the icon in the wrong location within the project files. Here's my code inside SysTray.java:

package systray;

import java.awt.*;

public class SysTray {

    public static void main(String[] args) {

        Runnable runner;
        runner = new Runnable() {

    public void run() {
      if (SystemTray.isSupported()) {
          SystemTray tray = SystemTray.getSystemTray();
          Image image = Toolkit.getDefaultToolkit().getImage("MyIcon.png");
          PopupMenu popup = new PopupMenu();
          MenuItem item = new MenuItem("A MenuItem");
          popup.add(item);
          TrayIcon trayIcon = new TrayIcon(image, "The Tip Text", popup);
          try {
            tray.add(trayIcon);
          } catch (AWTException e) {
            System.err.println("Can't add to tray");
          }
        } else {
          System.err.println("Tray unavailable");
        }

      }
    };
    EventQueue.invokeLater(runner);
  }
}

I placed "MyIcon.png" inside the same directory as the SysTray.java (main). But I actually guessed that's where I should place it.

Here's the directory structure:

SysTray/

  • Source Packages/ - systray/ - SysTray.java
  • Source Packages/ - systray/ - MyIcon.png
  • Libraries/

I see a blank space for the icon in the system tray, and when i hover my mouse I do see the "The Tip Text". But no icon.

I also received no error from NetBeans about a missing image. So, I'm stuck

coffeemonitor
  • 12,780
  • 34
  • 99
  • 149

2 Answers2

0

You can make your way work by just specifying an absolute path to the image e.g. "C:\MyProjects\projectname\packagename\MyIcon.png"

what you want though is to load the image as a resource instead of directly from the file system (for when you deploy as a jar). Like this:

String imageLocation = "/systray/MyIcon.png";
URL imageURL = this.getClass().getResource(imageLocation);
Image image = Toolkit.getDefaultToolkit().getImage(imageURL);

See How to correctly get image from 'Resources' folder in NetBeans for how to add a resource folder (for images, audio, etc).

Community
  • 1
  • 1
JohnKlehm
  • 2,368
  • 15
  • 9
0

Images Folder - Using NetBeans IDE 12.x

Save images e.g. 'Sample.png' in 'resources' folder which will be in the same hierarchy with 'java' folder. (The resources folder contents will be automatically extracted when building the project and sent to a jar file together with compiled classes.) The jar file will be stored to 'target' folder just under the parent project folder.

Next copy all the contents of resources folder manually to the project folder just as files in the project folder. (these will be used for code testing in the IDE)

For coding image access in the code e.g

img = ImageIO.read(new File("Sample.png"));  

Just type the image filename thats all needed. Build the project and you will see the images files included with java classes in the "target" folder, in a jar file. open the jar file to see compiled classes and the images.

Execute the class files normally using java command "java classname".

To run class files from command line first extract the jar to any location on the computer and cd to that folder. *** Ensure to copy resources folder contents directly to project folder all the time after updating or replacing resources ***

Any resources outside these folders will require extra code for handling the absolute path for those locations in the computer.