问题
[ Issue ] :
Seems like the behavior of SubScene or TabPane isn't what i was expecting or something is wrong with it.
Because Instead of Being Focused on Subscene [*1.] when i click on Subscene it focuses on TabPane. Even when i force it to focus on the SubScene it forces back & focuses on TabPane [*2.]
- (Is in grey color & it is inside a tab)
- (as seen on Debug console, 3rd GIF)
Replicating the Issue:
(How to test the issue by yourself)
- Add a TabPane & a Tab
- Add a SubScene Inside the Tab
- Check By Clicking On SubScene Where it Focuses By Printing:
SubScene.getScene().getFocusOwner().getId();
Main.java | Controller.Java | sample.fxml | (Or just Copy Paste)
.
Issue Effect:
| sub-scene |
| sub-scene.getScene() |
.
[ Answer ] :
Thanks to @Slaw:
Try scene.setOnMousePressed(e -> { scene.requestFocus(); e.consume(); }). If the mouse-pressed event reaches the tab pane (or something related to the tab pane, not sure) then it requests focus. And regarding naming conventions, variables use camelCase
Yes, the behavior makes sense. If the mouse is pressed somewhere inside a tab and none of the nodes consume the event then the tab pane should become focused. To understand more about event processing, such as what consume() does, see docs.oracle.com/javase/8/javafx/events-tutorial/events.htm
.
Outro:
Really Thanks Everyone who helped or even thought about helping [...],
George.
来源:https://stackoverflow.com/questions/57629097/answered-javafx-subscene-wont-focus-inside-tabpane-when-clicked