dustinkredmond / FXTrayIcon

Tray Icon implementation for JavaFX applications. Say goodbye to using AWT's SystemTray icon, instead use a JavaFX Tray Icon.
MIT License
327 stars 26 forks source link

A bug? Or am I doing something wrong? #57

Closed KaitlynEthylia closed 1 year ago

KaitlynEthylia commented 2 years ago

Menus appear to be getting added to the list in the wrong order.

This code:

Menu dictionary = new Menu("Dictionary");
dictionary.getItems().addAll(customDictionary, saveDictionary, loadDictionary, editDictionary);

trayIcon.addMenuItems(saveSettings, loadSettings);
trayIcon.addSeparator();
trayIcon.addMenuItems(keyListener, paused);
trayIcon.addMenuItem(dictionary);
trayIcon.addSeparator();
trayIcon.addMenuItems(show, hide);
trayIcon.addExitItem("Exit");

creates this menu: image

For some reason the menu dictionary is between "Show" and "Hide to Tray" when it should come just before the second separator.

Also this warning appears in the console, but I don't understand why as they are only added once.


Jul 10, 2022 12:32:03 PM javafx.scene.control.Menu$6 onChanged
WARNING: Adding MenuItem Custom Dictionary that has already been added to Keys
Jul 10, 2022 12:32:03 PM javafx.scene.control.Menu$6 onChanged
WARNING: Adding MenuItem Save Dictionary that has already been added to Keys
Jul 10, 2022 12:32:03 PM javafx.scene.control.Menu$6 onChanged
WARNING: Adding MenuItem Load Dictionary that has already been added to Keys
Jul 10, 2022 12:32:03 PM javafx.scene.control.Menu$6 onChanged
WARNING: Adding MenuItem Edit Dictionary that has already been added to Keys```
EasyG0ing1 commented 2 years ago

@KacyBiscuit

Try using the Builder class of FXTrayIcon, like this:

Menu dictionary = new Menu("Dictionary");
dictionary.getItems().addAll(customDictionary, saveDictionary, loadDictionary, editDictionary);

FXTrayIcon trayIcon = new FXTrayIcon.Builder(parentStage, iconFile, width, height)
  .menuItem(saveSettings)
  .menuItem(loadSettings)
  .separator()
  .menuItem(keyListener)
  .menuItem(paused)
  .menu(dictionary)
  .separator()
  .menuItem(show)
  .menuItem(hide)
  .addExitMenuItem("Exit")
  .show()
  .build();

It should put everything in the order that you have them placed in your build sentence.

Also, when you add any item to an existing FXTrayIcon, the library will verify that the label String assigned to the item does not already exist in the trayMenu, and if it does, it will throw an error.

EasyG0ing1 commented 2 years ago

@KacyBiscuit - It's been a while since you opened this incident, have the comments I made been of any use to you?

KaitlynEthylia commented 2 years ago

So sorry for the long reply! I had thought I'd replied before but I must've forgot as I've been busy recently, that helped greatly thanks! although I don't entirely understand the second part of your message as when I'm adding them they definately don't already exist so I don't see why it should throw an error

EasyG0ing1 commented 2 years ago

So sorry for the long reply! I had thought I'd replied before but I must've forgot as I've been busy recently, that helped greatly thanks! although I don't entirely understand the second part of your message as when I'm adding them they definately don't already exist so I don't see why it should throw an error

What I mean by saying "when you add any item to an existing FXTrayIcon" - I'm talking about after you have already created an instance of FXTrayIcon, and then you add menuItems into it (as opposed to adding all of your menuItems in your build sentence).

The only point I was trying to make, is that FXTrayIcon checks the label of every menuItem that you place into FXTrayIcon, and if it sees that the words in the label have already been used, then it will throw an error.

This example should make it clearer:

Let's say that I create a new instance of FXTI

FXTrayIcon trayIcon = new FXTrayIcon.Builder(stage, iconImage, imageWidth, imageHeight)
     .menuItem("Open File", e-> openFile())
     .menuItem("Close File", e-> closeFile())
     .separator()
     .addExitMenuItem("Exit", e-> closeProgram())
     .show();

Then after that object is already created, I then try to do something like this:

MenuItem openFile = new MenuItem("Open File");
openFile.setOnAction(e -> openFile());
trayIcon.addMenuItem(openFile);

FXTrayIcon will throw an error because it already has a menuItem with the label "Open File" in it.

Does that make sense?

I wasn't saying necessarily that you were trying to do this, I was just pointing out that if you were, then you would get an error ... that's all I was saying.

dustinkredmond commented 1 year ago

Has this issue been resolved?

KaitlynEthylia commented 1 year ago

so sorry, it has been but ive been logged out of github for a while so i havnt been receaving notifications

ctoabidmaqbool commented 1 year ago

best of luck!