Comments (8)
@mrgreywater When you're extending a class, then all of the protected methods of the class you're extending will be available in the extended (sub)class... with IntelliJ Idea those methods show up in autocomplete as you can see here
Screen.Recording.2023-02-08.at.10.10.25.AM.mov
Also, you could create in the subclass, a public method that returns the AWT TrayIcon object ... you can even expose the builder class and pick and chose which constructors you want to include in that exposed class...
import com.dustinredmond.fxtrayicon.FXTrayIcon;
import javafx.stage.Stage;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.net.URL;
public class TrayIcon extends FXTrayIcon {
public TrayIcon(Stage parentStage, File iconFile, int iconWidth, int iconHeight) {
super(parentStage, iconFile, iconWidth, iconHeight);
}
public void setMouseListener() {
getTrayIcon().addMouseListener(new MouseListener() {
@Override public void mouseClicked(MouseEvent e) {
if(e.getButton() == 3) {
System.out.println("User right clicked on tray icon");
}
}
@Override public void mousePressed(MouseEvent e) {
}
@Override public void mouseReleased(MouseEvent e) {
}
@Override public void mouseEntered(MouseEvent e) {
}
@Override public void mouseExited(MouseEvent e) {
}
});
}
public java.awt.TrayIcon trayIcon() {
return super.trayIcon;
}
public static class Builder extends FXTrayIcon.Builder {
public Builder(Stage parentStage, URL iconImagePath, int iconWidth, int iconHeight) {
super(parentStage, iconImagePath, iconWidth, iconHeight);
}
}
}
From there, you would just use your extended subclass in your application instead of using the library's FXTrayIcon class directly.
from fxtrayicon.
I much prefer what #74 does. If one is comfortable enough to use the AWT TrayIcon, I didn't envision them as using the FXTrayIcon library. I was mistaken on this, and could definitely see how folks may want to hack around with the AWT object. Planning on merging this PR. Thanks @EasyG0ing1!
from fxtrayicon.
I think I did this by design so as not to confuse folks that may see the getTrayIcon
method and not understand the AWT bits. Let me check my notes on this. Sub-classing FXTrayIcon might be preferable if you're dabbling around with the internals.
from fxtrayicon.
Here's an example of subclassing FXTrayIcon then from that subclass, setting up a listener for the right mouse button event:
import com.dustinredmond.fxtrayicon.FXTrayIcon;
import javafx.stage.Stage;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
public class TrayIcon extends FXTrayIcon {
public TrayIcon(Stage parentStage, File iconFile, int iconWidth, int iconHeight) {
super(parentStage, iconFile, iconWidth, iconHeight);
}
public void setMouseListener() {
getTrayIcon().addMouseListener(new MouseListener() {
@Override public void mouseClicked(MouseEvent e) {
if(e.getButton() == 3) {
System.out.println("User right clicked on tray icon");
}
}
@Override public void mousePressed(MouseEvent e) {
}
@Override public void mouseReleased(MouseEvent e) {
}
@Override public void mouseEntered(MouseEvent e) {
}
@Override public void mouseExited(MouseEvent e) {
}
});
}
}
And here is how you would use it:
File file = new File(pathToMyIconFile);
TrayIcon tray = new TrayIcon(stage,file,24,24);
tray.addExitItem("Exit");
tray.setMouseListener();
tray.show();
And here's what happens when the icon is right clicked:
User right clicked on tray icon
from fxtrayicon.
Thank you. Yes I'm doing something similar right now as I indicated. I still think just exposing the underlying Tray Icon would make it more straight forward.
As is I had to investigate the source code to find a way to attach that callback. Autocomplete doesn't show the getTrayIcon function as it is only available in the inherited context. Making it public would make it more "self-documenting" in my opinion.
from fxtrayicon.
@mrgreywater It looks like I was mistaken about exposing the TrayIcon awt object. It's not accessible outside of the subclassed context... but that shouldn't be a big deal since there really shouldn't be a need to access that object outside of that class in the first place. But when I tried exposing it, the IDE barked at me reminding me that no matter how I try to make it available outside of that class, I can go pound sand cause its not gonna happen.
I even tried making it an object within the subclass, then setting it equal to the protected get method then with a different method exposing it from there, but its protected status follows it where it's used so then the only way to accomplish that would be to possibly instantiate a new awt.TrayIcon object and then copy that one to the new one if that can even be done.
But like I said once you have it in the subclass... it really shouldn't be necessary to access it outside of the subclass.
from fxtrayicon.
@mrgreywater I submitted a PR that I think will meet the original design intent of protecting the object while also providing a way of accessing it without needing to extend the main class. Ultimately it's up to Dustin if he wants to accept the PR, but I think he might like the solution that I proposed.
from fxtrayicon.
@dustinkredmond @mrgreywater I already have a custom right-click option on an app I wrote that gives me access to my One Time Password keys right from the system tray. Now I can just right-click on it instead of navigating the menu to have the keys just pop up with that single click. I had wanted to do this from the beginning but just assumed I couldn't and never dug into it until this issue was opened. I'm loving the evolution of this library ... though I'm still waiting for the day when we will get native JavaFX access to the system tray, though I'm not holding my breath on that one.
from fxtrayicon.
Related Issues (20)
- usage in modular fx project? HOT 1
- Ubuntu 20.04 UnsupportedOperationException: SystemTray icons are not supported HOT 1
- NullPointerException when null MenuItem() HOT 3
- When another thread is running, some codding stops working! HOT 5
- After adding menu items, Actions listener on menu items not working HOT 2
- Clicking two time on Tray icon show empty stage? HOT 4
- After adding checkMenuItem, chech or uncheck not working HOT 1
- How to set Taskbar icon to JFXFileChooser? HOT 1
- Icon resolution limited to 16x16 on Windows HOT 1
- Only square icons possible? HOT 8
- addMenuItem after tray is shown throws exception HOT 8
- showMessage() title of the notification window HOT 6
- Icon Size Discussion HOT 1
- Blurry Icon with FXTrayIcon, Clearer Icon with AWT HOT 1
- Tray icon is non-transparent on some Linux desktop environments HOT 4
- JFXCentral Badge HOT 1
- TrayMenu can't show unicode characters HOT 1
- Maven central version HOT 3
- Menu size issue (high dpi) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fxtrayicon.