我遇到问题,我有 BorderPane
,其中中心有 TabPane
和具有各种其他 Nodes
的 Tabs
>(例如另一个 BorderPane
)。
当我尝试使用时:
parentBorderPane.setOnMouseDragged(e -> System.out.println("DRAGGING"));
parentBorderPane.setOnMouseMoved(e -> System.out.println("MOVING"));
...事件永远不会发生(我可以在当前打开的 Tab
内容上捕获它们,但这不是我想要的)。
我需要知道此类事件何时在两个地方发生,(第一个事件通过 Tab
的内容,然后通过包含 TabPane
,然后通过主父 边框 Pane
)。所以我不能(对任何 child )使用:
this.setMouseTransparent(true);
如何解决这个问题?
编辑
让我补充一下,主要问题在于 TabPane
- 它不会将事件传递到堆栈中的下一个节点。
编辑 2
public class T extends Application
{
public static void main(String[] args) throws Exception
{
launch(args);
}
@Override
public void start(Stage stage) throws Exception
{
stage.setTitle("Test");
Scene scene = new Scene(this.working()); // change to this.problem();
stage.setScene(scene);
stage.show();
}
private BorderPane working()
{
BorderPane main = new BorderPane();
main.setPrefSize(800, 600);
main.setOnMouseMoved(e -> System.out.println("MOVING ON MAIN"));
BorderPane sub = new BorderPane();
sub.setOnMouseMoved(e -> System.out.println("MOVING ON SUB"));
main.setCenter(sub);
return main;
}
private BorderPane problem()
{
BorderPane main = new BorderPane();
main.setPrefSize(800, 600);
main.setOnMouseMoved(e -> System.out.println("MOVING ON MAIN"));
BorderPane sub = new BorderPane();
sub.setOnMouseMoved(e -> System.out.println("MOVING ON SUB"));
main.setCenter(sub);
TabPane tabs = new TabPane();
tabs.setOnMouseMoved(e -> System.out.println("MOVING ON TABS"));
sub.setCenter(tabs);
return main;
}
}
working()
- 相互堆叠的正常 Panes
将按其应有的方式工作(从堆栈顶部到底部传递事件)。
problem()
- 因为 TabPane
是 Control 的子类(我目前正在研究),它会停止鼠标事件并且不让 parent 看到它们。
最佳答案
我找到了解决方案 - 而不是尝试在 Control
(TabPane
的父类)消耗它的冒泡阶段(子级->父级后面)接收事件,我可以使用event filters并在事件消耗之前捕获事件。
关于java - 使鼠标事件在整个节点堆栈上可检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47160858/