Thursday 15 November 2012

Disabling Focus in JavaFX

Had the need to disable focus on a ScrollPane (in the image viewer of the previous post) - for simplicity I wanted to keep the focus elsewhere.

Although one can set FocusTraversable to false and the TAB key will no longer iterate through it, it still gets focus when you click on it.

I had to sub-class the ScrollPane and override requestFocus to NO-OP.

  scroll = new ScrollPane() {
    public void requestFocus() { }
  };

Obviously something to use judiciously, as it removes the ability for keyboard control of that Node.

1 comment:

Unknown said...

It works, now I can disable focusing on a ScrollPane when I overrided the ScrollPane's method requestFocus()!
Thank you so much!