Search code examples
javafxviewkotlinlistenertornadofx

Tornadofx onFocus listener


Is there something like an onFocus() method, which I could override like the onDock() and onCreate() in the view class?

In the documentation there is only written about live reloading of views.

I tried combining that with, the onDock() and onCreate() method, but, even though it "works", it's not a very neat way of replicating onFocus behaviour.

Is there a simple way to have a "listener" method that's called when the view/fragment comes to front/on focus?


Solution

  • The View is a container, and not an UI element in the sense of the JavaFX context, so it cannot receive an onFocus callback. However, you can register one with the current window or even the root node of the View. If you're opening a window, you could register such an even with the currentWindow property. If you're not opening a window, you could register it with the root property of the View. Here is an example listening for a single focus change event from the currentWindow:

    override fun onDock() {
        currentStage?.focusedProperty()?.onChangeOnce {
    
        }
    }