-
1. Re: Creating a custom context for JavaFX
luksa Jul 1, 2013 4:14 PM (in response to philipp91)1 of 1 people found this helpfulWhen a button is clicked, the button event (or whatever it's called) has a reference to the button. You can then traverse up the component tree from the button up until you find the window. Storing this window in a threadLocal would allow your context to know what beans to return. Of course you do need to make sure you set the threadlocal before any of your application's button listeners are called.
-
2. Re: Creating a custom context for JavaFX
philipp91 Jul 1, 2013 4:34 PM (in response to luksa)That would require me to do that for every button I need to use for this, right? I'm pretty sure it would work that way. But if I need to have some code for every button, I wouldn't need any dependency injection in the first place - I could just work with local variables in the window and retrieve them after traversing up the component tree.
-
3. Re: Creating a custom context for JavaFX
luksa Jul 2, 2013 3:26 AM (in response to philipp91)You can probably avoid adding a listener to every button by hooking into the class that actually dispatches events. In Swing, you can do this through Toolkit.getDefaultToolkit().getSystemEventQueue().push(new MyEventQueue()). JavaFX probably has something similar to this.
Dependency injection (or even just a simple ThreadLocal without DI) would allow you to access the window in which the event occured in any class that needs it. Without the ThreadLocal, you'd have to pass a reference pointing to the window to every method that needs it.