Code Monkey home page Code Monkey logo

Comments (8)

EasyG0ing1 avatar EasyG0ing1 commented on September 15, 2024 1

@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.

dustinkredmond avatar dustinkredmond commented on September 15, 2024 1

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.

dustinkredmond avatar dustinkredmond commented on September 15, 2024

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.

EasyG0ing1 avatar EasyG0ing1 commented on September 15, 2024

@mrgreywater

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.

mrgreywater avatar mrgreywater commented on September 15, 2024

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.

EasyG0ing1 avatar EasyG0ing1 commented on September 15, 2024

@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.

EasyG0ing1 avatar EasyG0ing1 commented on September 15, 2024

@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.

EasyG0ing1 avatar EasyG0ing1 commented on September 15, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.