Answered - JavaFX: SubScene won't focus inside TabPane when clicked? [closed]

馋奶兔 提交于 2020-01-04 06:40:28

问题


[ 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.]

  1. (Is in grey color & it is inside a tab)
  2. (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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!