How do I customize the program name in a traybar notification in AWT?
Asked Answered
D

4

14

AWT's TrayIcon class has a method called displayMessage that shows a native OS message that in Windows 10 looks like this:

enter image description here

when called like this:

Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/isotype.png"));
final TrayIcon trayIcon = new TrayIcon(image, appName());
trayIcon.displayMessage("Caption", "Text text text", TrayIcon.MessageType.INFO);

How do I customize the string "Java(TM) Platform SE binary". When I build a self-contained exe for my application, it instead reads "Blah.exe" while I'd prefer it if reads just "Blah".

To package the application I'm using the JavaFX toolchain through the excellent JavaFX-Gradle-Plugin.

Daddy answered 12/12, 2017 at 1:56 Comment(3)
Use launch4j Cross-platform Java executable wrapper (a custom launcher)Plug
@aKilleR: that means changing my whole installer toolchain. What do I gain by using launch4j?Daddy
@Pablo, does this help? #13683835Door
T
5

One workaround is to use TrayIcon.MessageType.NONE. In this case you won't get the last line at all, but you won't get any INFO, WARNING or ERROR icon either.

But the good thing is that you can get your application icon in the displayed message. If you create the TrayIcon with some image like TrayIcon trayIcon = new TrayIcon(image, "Tooltip") and then package your application with JavaFX-Gradle-Plugin, displayMessage method will reuse this image and you will get something like this:

Tuberose answered 17/12, 2017 at 1:32 Comment(5)
I'm actually getting the icon of my application in there, a small version, but it looks good; so, so far, this seems to be the best solution.Daddy
@Pablo You will still get your application icon in the system tray. I just meant that the round info icon will disappearTuberose
Yes, the round icon is gone, but my apps icon appear in the notification: i.imgur.com/uvcFT6i.pngDaddy
I don't know, it just happened like that. My guess is that it's because that's the tray icon I created and it's re-using it.Daddy
Essentially doing this: docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.htmlDaddy
A
3

On my system (tried both Java 8u151 and Java 9.0.1 on Windows 10), with the minimal setup of build.gradle from javafx-gradle-plugin, I have not found any configuration option for that name, but I can control it: the packager simply takes the name of parent directory that contains my sources.

Example with "Foo Bar":
Executable name = "FooBar.exe"
Displayed name = "Foo Bar"
enter image description here

(What I don't get, then, is why you see "Blah.exe" instead of the name of your parent directory...)

Archway answered 16/12, 2017 at 22:58 Comment(0)
M
2

To change the pop-up text which in your posted screenshot is displayed as Java(TM) Platform SE binary in your Blah.exe to Blah you could use for example the Resource Hacker™.

Change in the section Version Info the value for FileDescription to Blah either interactive in the GUI or on commandline.

Find below a simple example which needs to be amended for your needs.

versioninfo.rc resource script containing the information for the VERSIONINFO resource

1 VERSIONINFO
{
BLOCK "StringFileInfo"
{
    BLOCK "00000409"
    {
        VALUE "FileDescription", "Blah"
    }
}

BLOCK "VarFileInfo"
{
    VALUE "Translation", 0x0000 0x0409
}
}

compile the resource script to a resource file

ResourceHacker.exe -open versioninfo.rc \
    -save versioninfo.res \
    -action compile 
    -log CONSOLE

add the resource to the executable

ResourceHacker.exe -open Blah.exe \
    -save Blah_new.exe \
    -resource versioninfo.res \
    -action addoverwrite \
    -mask VERSIONINFO,1,0 \
    -log CONSOLE

For completeness here the command to extract the VERSIONINFO from an EXE file.

 ResourceHacker.exe -open Blah.exe \
     -save versioninfo.rc \
     -action extract \
     -mask VERSIONINFO,, \
     -log CONSOLE
Macaroon answered 15/12, 2017 at 12:37 Comment(4)
The problem with this is that I cannot do anything to the .exe because in my build chain, creating the .exe and packaging it in the installer is an atomic operation (that's how javafxpackager works, which is what JavaFX-Gradle-plugin uses).Daddy
@Pablo As far as I know the javafxpackager doesn't create an exe file. The Gradle plugin uses Inno Setup (for EXE installers) or WiX (for MSI installers).Macaroon
Fair enough, it's the plug in that makes it an atomic operation and not javafxpackager.Daddy
@Pablo Would you mind to provide a MCVE. Based on the answer of @Hugues M. it seems not to be the default behaviour. When I build the minimal-setup-jfxnative example provided by the JavaFX-Gradle-Plugin the created exe file does not even contain a FileDescription value. And the text Java(TM) Platform SE binary, in your screenshot, is comming from the java.exe / javaw.exe executable. So it would be interesting to reproduce your observed behaviour.Macaroon
R
2

The behaviour of TrayIcon is platform dependent. On a Mac, there's no such thing as "Java(TM) Platform SE binary", nor the executable.

If you want to change the behaviour on your platform, I guess you could play with awt.toolkit. See https://docs.oracle.com/javase/8/docs/api/java/awt/Toolkit.html#getDefaultToolkit--

Riocard answered 16/12, 2017 at 21:50 Comment(1)
I don't see anything in there that allows me to modify that name.Daddy

© 2022 - 2024 — McMap. All rights reserved.