B4J Library jFXSystemTrayHandler - Display a javafx context menu

This is a code module that allows display of a JavaFX Context Menu when the system tray Icon is clicked.

Used in conjunction with the MenuManager Library, it is possible to add checkbox items, images, Fontawesome and MaterialIcons and CustomItems to the contextmenu.

You can compile it to a library if you prefer not to add the module to your project.

Untested on a Mac or Linux, but please let me know.

Demo app depends on:

jSystemTray
MenuManager
JavaObject

Update to 0.2 - Changed Form type to UTILITY - Confirmed working with MacOS

14
 

Attachments

  • SystemTrayHandler0.2.zip
    17.9 KB · Views: 525
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Nice job!

I see that it creates a new form when clicked. Try to set its style to UTILITY. This will hide the task bar item from appearing each time.

It works on Mac with a few issues:
1. The form doesn't show on the first click.
2. It doesn't hide automatically. Only when an item is selected. A possible workaround is to hide it when clicking on the icon itself.
3. The menu can be hidden by other windows. It is not brought to front.
 

stevel05

Expert
Licensed User
Longtime User
Thanks Erel, I'll try and boot up an old Mac I have tomorrow (if it still works). I think it'll run java 8.
 

stevel05

Expert
Licensed User
Longtime User
Finally got something working on Mac, Running Mavericks and java 1.8.0_171 it seems to work the same as on the PC.

What OS and java versions are you testing it on?
 

stevel05

Expert
Licensed User
Longtime User
Fixing the mac implementation has stalled for the moment as I haven't been able to get access to a more recent system. I'm working till after the weekend, I'll look at it again next week.
 

stevel05

Expert
Licensed User
Longtime User
OK, finally got it to fail on Mavericks, would you believe that setting the form type to UTILITY changed the behaviour?. So as originally released with form set to TRANSPARENT, it exhibits the issues that Erel reported, set to UTILITY, it doesn't. And of course I made that change before I tested it on Mavericks.

So would someone be so kind as to test this version, the only change is the form type and see if that does actually work properly on a newer OS than Mavericks.

What should happen:
  • Click on the orange icon in the icon bar (don't know what it's called in MacOS) and the menu is displayed.
  • Click anywhere not on the menu, the menu is closed
  • Click on an option, the option is logged and the menu closes.
  • Click on a sub menu the sub menu opens
    • If a sub menu is open then
      • Click anywhere not on the menu, the menu is closed
      • Click on an option, the option is logged and the menu closes.

Thanks

Edit: File moved to first post
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Thanks Erel.

I have moved the file to the first post.
 

stevel05

Expert
Licensed User
Longtime User
For completeness, I tried to test this on Ubuntu, but apparently the system tray was removed a few releases ago, so it is not currently supported. It is possible to jump through some hoops to re-install it but I don't want to go there. If anybody tries in on flavours of Linux that supports the system tray, please let me know the results.
 

Blueforcer

Well-Known Member
Licensed User
Longtime User
On mac opening the Traymenu wont work when you pack the app with B4J Packager. It crash
This is the same problem as mentioned here:
https://www.b4x.com/android/forum/threads/problem-with-b4jpackager.96667/#post-609919

If i set the Owner of the STForm (in FXSystemTrayHandler) to the Mainform, it works. But only if the Mainform is visible.
But of course i want to hide the Mainform, to use the Tray ;)

is there a way to solve this?
 

stevel05

Expert
Licensed User
Longtime User
Does it work as expected if you run the jar file without packaging it?
 

Blueforcer

Well-Known Member
Licensed User
Longtime User
Yes.
Maybe because i run Java 10, and the packager packing java 8 wich still has this problem
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Hmm, the work around given in the JDK bug report would still leave an icon in the task bar, which is not what you want. I guess it's not going to do what you want until the packager allows the use of java 10.
 

Blueforcer

Well-Known Member
Licensed User
Longtime User
yes i think so too, ive opened a new thread for that.
For now i will publish my App as the raw Jar file for MacOS.
Thank you anyway
 

prajinpraveen

Active Member
Licensed User
Longtime User
Good One.

When used with an additional monitor, The menu displays on the second screen and not next to the tray icon
 

stevel05

Expert
Licensed User
Longtime User
I have two monitors and it seems to work OK for me.

What OS are you running, java version?
 

stevel05

Expert
Licensed User
Longtime User
I've tried it with Java 8 and Java 11, on windows 10..

Without knowing what hardware you have installed, the only thing I can suggest is that you check the X value returned in FXSystemtrayHandler Module TIMouse_Event sub and see if it looks reasonable. If not, there may be alternative methods in the javaFX mouse event that will return a better value.

If it does look reasonable, then I would suggest that you make sure you have the latest drivers installed for your graphics card and take a look on Google to see if anyone has had a similar experience.

As I say, it is working as expected here with two monitors. Are your two monitors the same resolution?
 
Last edited:
Top