As your CustomControl
uses composition, you can delegate to the focus properties of each TextField
. Given two instances,
private final TextField tf1 = new TextField("One");
private final TextField tf2 = new TextField("Two");
The implementation of an instance method isFocused()
is then straightforward:
private boolean isFocused() {
return tf1.isFocused() | tf2.isFocused();
}
Add focus listeners as shown here to see the effect.
tf1.focusedProperty().addListener((Observable o) -> {
System.out.println(isFocused());
});
tf2.focusedProperty().addListener((Observable o) -> {
System.out.println(isFocused());
});
This can't be done. The whole problem is that isFocused()
is final
in Node
.
It seems you wanted to override isFocused()
in CustomControl
, but that is not possible for a final
method and it would violate the notion of a single component having focus. As CustomControl
is a composite, you'll need to manage focus internally. You may want to use a custom FocusModel
as seen in ListView
.