Javafx8 stackpane children 阻止鼠标事件
Javafx8 stackpane children blocking mouse events
我有一个具有复杂场景的 javafx8 应用程序。它由一个拆分窗格组成,在顶部部分我有一个具有多级 (children) 的堆栈窗格。最靠后的是一个只需要在调整大小时重绘的背景,另一个带有网格线,lat/long。另一个用于绘图,最后,另一个带有滑动复合控件。当组合(按钮、标签、组合框和文本字段,在堆栈框架的最后 child 中,它正确拦截并处理鼠标和键盘事件。但是,在顶部没有事件传播到 child 下面的窗格。我切换了顺序,现在按钮和轴控件可以工作,但现在没有任何内容传播到它下面的复合控件。按钮看不到鼠标操作。
这似乎与 Stackpane - MouseClick to be listened by both its children 相似,但不清楚是否有正确答案。
我通过一个非常简单的测试简化了我的场景,源码如下。在这里我有一个 StackPane 有两个 children,两个 VBox,一个左上对齐,另一个右上对齐。只有右上角的第二个按钮响应被按下。
为什么。我的应用程序具有多个窗格的原因是性能。两种绘图操作都非常昂贵且频繁,我不想不断重绘静态或接近静态的布局。
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<StackPane fx:id="containerPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mouseeventdemo.MouseEventDemoController">
<children>
<VBox fx:id="container1" prefHeight="200.0" prefWidth="100.0">
<children>
<Button fx:id="leftButton" mnemonicParsing="false" onAction="#leftButtonDown" text="left button" />
</children>
</VBox>
<VBox fx:id="container2" alignment="TOP_RIGHT" prefHeight="200.0" prefWidth="100.0">
<children>
<Button fx:id="rightButton" mnemonicParsing="false" onAction="#rightButtonDown" text="Right Button" />
</children>
</VBox>
</children>
</StackPane>
控制器:
package mouseeventdemo;
/**
* Sample Skeleton for 'FXMLDocument.fxml' Controller Class
*/
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
public class MouseEventDemoController {
@FXML // ResourceBundle that was given to the FXMLLoader
private ResourceBundle resources;
@FXML // URL location of the FXML file that was given to the FXMLLoader
private URL location;
@FXML // fx:id="containerPane"
private StackPane containerPane; // Value injected by FXMLLoader
@FXML // fx:id="container1"
private VBox container1; // Value injected by FXMLLoader
@FXML // fx:id="leftButton"
private Button leftButton; // Value injected by FXMLLoader
@FXML // fx:id="container2"
private VBox container2; // Value injected by FXMLLoader
@FXML // fx:id="rightButton"
private Button rightButton; // Value injected by FXMLLoader
@FXML
void leftButtonDown(ActionEvent event) {
System.out.println("leftButtonPressed");
}
@FXML
void rightButtonDown(ActionEvent event) {
System.out.println("rightButtonPressed");
}
@FXML // This method is called by the FXMLLoader when initialization is complete
void initialize() {
assert containerPane != null : "fx:id=\"containerPane\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert container1 != null : "fx:id=\"container1\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert leftButton != null : "fx:id=\"leftButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert container2 != null : "fx:id=\"container2\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert rightButton != null : "fx:id=\"rightButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
}
}
主要
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mouseeventdemo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author walt
*/
public class MouseEventDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
在我的应用程序中,最后面的窗格是背景 canvas。它只需要绘制一次,并在 window 调整大小时重新绘制。
下一个是具有 lat/long 个网格的 canvas。仅当 window 大小改变或垂直或水平比例改变时才需要重新绘制。
接下来是主图canvas在下面
<vbox>
<hbox>
drawing canvas
vertical scale
</hbox>
horizontal scale
</vbox>
下一个带有一个或多个旗杆,其中垂直杆是像十字线一样的计算尺。这些需要是可拖动的。如果这一层在顶部,它可以完美滑动,但两个刻度无法获得焦点。如果顶部的两个窗格颠倒,则秤可以正常工作,但旗杆 objects 永远不会接收到事件。
两个按钮示例非常简单地复制了我所看到的内容。
谢谢!
在javafx的事件处理中,只有mouseposition下的最顶层节点(在你的例子container2中)将被确定为鼠标点击事件的目标,这就是container1没有接收到该事件的原因。
目标节点是事件分发链中的结束节点。当您单击 leftButton 时,鼠标单击事件从场景图的根节点传播到目标节点 (container2),如果没有被消耗则返回。 Container1 不在调度链上,不会收到事件:
http://docs.oracle.com/javafx/2/events/processing.htm
你可以尝试使用container.setPickOnBounds(false)
我有一个具有复杂场景的 javafx8 应用程序。它由一个拆分窗格组成,在顶部部分我有一个具有多级 (children) 的堆栈窗格。最靠后的是一个只需要在调整大小时重绘的背景,另一个带有网格线,lat/long。另一个用于绘图,最后,另一个带有滑动复合控件。当组合(按钮、标签、组合框和文本字段,在堆栈框架的最后 child 中,它正确拦截并处理鼠标和键盘事件。但是,在顶部没有事件传播到 child 下面的窗格。我切换了顺序,现在按钮和轴控件可以工作,但现在没有任何内容传播到它下面的复合控件。按钮看不到鼠标操作。
这似乎与 Stackpane - MouseClick to be listened by both its children 相似,但不清楚是否有正确答案。
我通过一个非常简单的测试简化了我的场景,源码如下。在这里我有一个 StackPane 有两个 children,两个 VBox,一个左上对齐,另一个右上对齐。只有右上角的第二个按钮响应被按下。
为什么。我的应用程序具有多个窗格的原因是性能。两种绘图操作都非常昂贵且频繁,我不想不断重绘静态或接近静态的布局。
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<StackPane fx:id="containerPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mouseeventdemo.MouseEventDemoController">
<children>
<VBox fx:id="container1" prefHeight="200.0" prefWidth="100.0">
<children>
<Button fx:id="leftButton" mnemonicParsing="false" onAction="#leftButtonDown" text="left button" />
</children>
</VBox>
<VBox fx:id="container2" alignment="TOP_RIGHT" prefHeight="200.0" prefWidth="100.0">
<children>
<Button fx:id="rightButton" mnemonicParsing="false" onAction="#rightButtonDown" text="Right Button" />
</children>
</VBox>
</children>
</StackPane>
控制器:
package mouseeventdemo;
/**
* Sample Skeleton for 'FXMLDocument.fxml' Controller Class
*/
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
public class MouseEventDemoController {
@FXML // ResourceBundle that was given to the FXMLLoader
private ResourceBundle resources;
@FXML // URL location of the FXML file that was given to the FXMLLoader
private URL location;
@FXML // fx:id="containerPane"
private StackPane containerPane; // Value injected by FXMLLoader
@FXML // fx:id="container1"
private VBox container1; // Value injected by FXMLLoader
@FXML // fx:id="leftButton"
private Button leftButton; // Value injected by FXMLLoader
@FXML // fx:id="container2"
private VBox container2; // Value injected by FXMLLoader
@FXML // fx:id="rightButton"
private Button rightButton; // Value injected by FXMLLoader
@FXML
void leftButtonDown(ActionEvent event) {
System.out.println("leftButtonPressed");
}
@FXML
void rightButtonDown(ActionEvent event) {
System.out.println("rightButtonPressed");
}
@FXML // This method is called by the FXMLLoader when initialization is complete
void initialize() {
assert containerPane != null : "fx:id=\"containerPane\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert container1 != null : "fx:id=\"container1\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert leftButton != null : "fx:id=\"leftButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert container2 != null : "fx:id=\"container2\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert rightButton != null : "fx:id=\"rightButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
}
}
主要
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mouseeventdemo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author walt
*/
public class MouseEventDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
在我的应用程序中,最后面的窗格是背景 canvas。它只需要绘制一次,并在 window 调整大小时重新绘制。
下一个是具有 lat/long 个网格的 canvas。仅当 window 大小改变或垂直或水平比例改变时才需要重新绘制。
接下来是主图canvas在下面
<vbox>
<hbox>
drawing canvas
vertical scale
</hbox>
horizontal scale
</vbox>
下一个带有一个或多个旗杆,其中垂直杆是像十字线一样的计算尺。这些需要是可拖动的。如果这一层在顶部,它可以完美滑动,但两个刻度无法获得焦点。如果顶部的两个窗格颠倒,则秤可以正常工作,但旗杆 objects 永远不会接收到事件。
两个按钮示例非常简单地复制了我所看到的内容。
谢谢!
在javafx的事件处理中,只有mouseposition下的最顶层节点(在你的例子container2中)将被确定为鼠标点击事件的目标,这就是container1没有接收到该事件的原因。
目标节点是事件分发链中的结束节点。当您单击 leftButton 时,鼠标单击事件从场景图的根节点传播到目标节点 (container2),如果没有被消耗则返回。 Container1 不在调度链上,不会收到事件: http://docs.oracle.com/javafx/2/events/processing.htm
你可以尝试使用container.setPickOnBounds(false)