Javafx 2 如何将事件从一个节点发送到另一个节点

Javafx 2 如何将事件从一个节点发送到另一个节点,javafx-2,Javafx 2,我有一个窗格,可以监听滚轮鼠标滚动;我还有一个滚动条,可以自动监听滚轮鼠标滚动。我想知道如何将窗格捕获的事件发送到滚动条 我不能使用滚动窗格,因为我需要一个自定义的窗格实现,我已经尝试使用滚动窗格,但它不能满足我的需要 我试图触发事件和其他方法,但我只能将事件传递/传播/发送到滚动条 提前谢谢 示例应用程序: package com.test; import javafx.application.Application; import javafx.beans.value.ChangeList

我有一个窗格,可以监听滚轮鼠标滚动;我还有一个滚动条,可以自动监听滚轮鼠标滚动。我想知道如何将窗格捕获的事件发送到滚动条

我不能使用滚动窗格,因为我需要一个自定义的窗格实现,我已经尝试使用滚动窗格,但它不能满足我的需要

我试图触发事件和其他方法,但我只能将事件传递/传播/发送到滚动条

提前谢谢

示例应用程序:

package com.test;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class ScrollTest extends Application{

   @Override
   public void start(Stage primaryStage) throws Exception {


     final BorderPane root = new BorderPane();

     final Pane pane = new Pane();
     root.setCenter(pane);

     final ScrollBar scrollBar = new ScrollBar();
     root.setRight(scrollBar);


     pane.addEventFilter(ScrollEvent.ANY, new EventHandler<ScrollEvent>(){
        @Override
        public void handle(ScrollEvent arg0) {
           System.out.println("scrolling on the pane");

           Event.fireEvent(scrollBar, arg0);
//           scrollBar.getEventDispatcher().dispatchEvent(arg0, arg1)
        }
     });



     scrollBar.setOrientation(Orientation.VERTICAL);
     scrollBar.valueProperty().addListener(new ChangeListener<Number>() {
        @Override
        public void changed(ObservableValue<? extends Number> ov, Number old_val, final Number new_val) {
           System.out.println("scrollBar.valueProperty() changed");
        }
     });


     primaryStage.setScene(new Scene(root, 600, 600));
     primaryStage.show();
   }

   public static void main(String[] args) {
      launch(args);
   }

}
package.com.test;
导入javafx.application.application;
导入javafx.beans.value.ChangeListener;
导入javafx.beans.value.observeValue;
导入javafx.event.event;
导入javafx.event.EventHandler;
导入javafx.geometry.Orientation;
导入javafx.scene.scene;
导入javafx.scene.control.ScrollBar;
导入javafx.scene.input.ScrollEvent;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.layout.Pane;
导入javafx.stage.stage;
公共类ScrollTest扩展了应用程序{
@凌驾
public void start(Stage primaryStage)引发异常{
最终边界窗格根=新边界窗格();
最终窗格=新窗格();
root.setCenter(窗格);
最终滚动条滚动条=新滚动条();
root.setRight(滚动条);
addEventFilter(ScrollEvent.ANY,new EventHandler()){
@凌驾
公共无效句柄(ScrollEvent arg0){
System.out.println(“在窗格上滚动”);
fireEvent(滚动条,arg0);
//scrollBar.getEventDispatcher().dispatchEvent(arg0,arg1)
}
});
滚动条。设置方向(方向。垂直);
scrollBar.valueProperty().addListener(新的ChangeListener()){
@凌驾

public void changed(ObservalEvalue看来,您的问题相当于定制滚动窗格实现的需要

我认为,一种可能的方法是解析事件,并使滚动条在事件发生时更改其值(在窗格上的事件上添加侦听器,解析是向上滚动还是向下滚动,并使用滚动条的递增或递减方法)

此外,我们还使用了一些用于滚动事件发送的实现。请将此草稿用于您的实现:

/**
 * Send ScrollEvent in the center of the control
 *
 * @param wrap Wrap, which will receive event
 * @param scrollX Number of pixels to scroll by x coordinate
 * @param scrollY Number of pixels to scroll by y coordinate
 */
protected static void sendScrollEvent(final Scene scene, Node node, double scrollX, double scrollY) {
    double x = node.getLayoutBounds().getWidth() / 4;
    double y = node.getLayoutBounds().getHeight() / 4;
    sendScrollEvent(scene, node, scrollX, scrollY, ScrollEvent.HorizontalTextScrollUnits.NONE, scrollX, ScrollEvent.VerticalTextScrollUnits.NONE, scrollY, x, y, node.getLayoutBounds().getMinX() + x, node.getLayoutBounds().getMinY() + y);
}

protected static void sendScrollEvent(final Scene scene, final Node node,
        double _scrollX, double _scrollY,
        ScrollEvent.HorizontalTextScrollUnits _scrollTextXUnits, double _scrollTextX,
        ScrollEvent.VerticalTextScrollUnits _scrollTextYUnits, double _scrollTextY,
        double _x, double _y,
        double _screenX, double _screenY) {
    //For 2.1.0 :
    //final ScrollEvent scrollEvent = ScrollEvent.impl_scrollEvent(_scrollX, _scrollY, _scrollTextXUnits, _scrollTextX, _scrollTextYUnits, _scrollTextY, _x, _y, _screenX, _screenY, false, false, false, false);
    //For 2.2.0 :
    //Interpretation: EventType<ScrollEvent> eventType, double _scrollX, double _scrollY, double _totalScrollX, double _totalScrollY, HorizontalTextScrollUnits _scrollTextXUnits, double _scrollTextX, VerticalTextScrollUnits _scrollTextYUnits, double _scrollTextY, int _touchPoints, double _x, double _y, double _screenX, double _screenY, boolean _shiftDown, boolean _controlDown, boolean _altDown, boolean _metaDown, boolean _direct, boolean _inertia)
    //For 8.0 before b64 and RT-9383
    //final ScrollEvent scrollEvent = new ScrollEvent.impl_scrollEvent(ScrollEvent.SCROLL, _scrollX, _scrollY, _scrollX, _scrollY, _scrollTextXUnits, _scrollTextX, _scrollTextYUnits, _scrollTextY, 0, _x, _y, _screenX, _screenY, false, false, false, false, false, false);

    //new ScrollEvent(EventType<ScrollEvent> eventType, 
    //double x, double y, double screenX, double screenY, 
    //boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown, 
    //boolean direct, boolean inertia, double deltaX, double deltaY, double gestureDeltaX, double gestureDeltaY, 
    //ScrollEvent.HorizontalTextScrollUnits textDeltaXUnits, double textDeltaX, 
    //ScrollEvent.VerticalTextScrollUnits textDeltaYUnits, double textDeltaY, int touchCount)
    final ScrollEvent scrollEvent = new ScrollEvent(ScrollEvent.SCROLL,
            _x, _y, _screenX, _screenY,
            false, false, false, false,
            false, false, _scrollX, _scrollY, 0, 0,
            _scrollTextXUnits, _scrollTextX,
            _scrollTextYUnits, _scrollTextY, 0, null /* PickResult?*/);

    Point2D pointOnScene = node.localToScene(node.getLayoutBounds().getWidth() / 4, node.getLayoutBounds().getHeight() / 4);
    final PickResultChooser result = new PickResultChooser();
    scene.getRoot().impl_pickNode(new PickRay(pointOnScene.getX(), pointOnScene.getY()), result);
    Node nodeToSendEvent = result.getIntersectedNode();
    nodeToSendEvent.fireEvent(scrollEvent);
}
/**
*在控件的中心发送ScrollEvent
*
*@param wrap,将接收事件
*@param scrollX要按x坐标滚动的像素数
*@param scrollY按y坐标滚动的像素数
*/
受保护的静态无效sendScrollEvent(最终场景、节点节点、双scrollX、双scrollY){
double x=node.getLayoutBounds().getWidth()/4;
双y=node.getLayoutBounds().getHeight()/4;
sendScrollEvent(场景、节点、scrollX、scrollY、ScrollEvent.HorizontalTextScrollUnits.NONE、scrollX、ScrollEvent.VerticalTextScrollUnits.NONE、scrollY、x、y、node.getLayoutBounds().getMinX()+x、node.getLayoutBounds().getMinY()+y);
}
受保护的静态无效事件(最终场景、最终节点、,
双卷,双卷,
ScrollEvent.HorizontalTextScrollUnits\u scrollTextXUnits、double\u scrollTextX、,
ScrollEvent.VerticalTextScrollUnits\u scrollTextYUnits,double\u scrollTextY,
双倍x,双倍y,
双屏幕(X,双屏幕){
//对于2.1.0:
//final ScrollEvent ScrollEvent=ScrollEvent.impl ScrollEvent(\u scrollX、\u scrollY、\u scrollTextXUnits、\u scrollTextX、\u scrollTextYUnits、\u scrollTextY、\u screenX、\u screenX、\u screenY、false、false、false);
//对于2.2.0:
//解释:EventType EventType、double\u scrollX、double\u scrollY、double\u totalScrollX、HorizontalTextScrollUnits\u scrollTextXUnits、double\u scrollTextX、VerticalTextScrollUnits\u scrollTextYUnits、double\u scrollTextY、int触点、double\u x、double\u screenX、double\u screenX、double\u screenY、double\u screenY、double\u screenX、double\u screenY、double_controlDown、布尔值altDown、布尔值metaDown、布尔值direct、布尔值惰性)
//对于b64和RT-9383之前的8.0
//final ScrollEvent ScrollEvent=新的ScrollEvent.impl\u ScrollEvent(ScrollEvent.SCROLL、\u scrollX、\u scrollX、\u scrollX、\u scrollX、\u scrollX、\u scrollX、\u scrollX、\u scrollX、\u scrollX、\u scrollX、\u scrollX、\u scrollX、\u scrollX、\u scrollX、\u scrollX、\u scrollX、;
//新的ScrollEvent(事件类型EventType,
//双x,双y,双x,双y,
//boolean shiftDown、boolean controlDown、boolean altDown、boolean metaDown、,
//布尔直接,布尔惯性,双deltaX,双deltaY,双gestureDeltaX,双gestureDeltaY,
//ScrollEvent.HorizontalTextScrollUnits textDeltaXUnits,双textDeltaX,
//ScrollEvent.VerticalTextScrollUnits textDeltaYUnits、double textDeltaY、int touchCount)
final ScrollEvent ScrollEvent=新的ScrollEvent(ScrollEvent.SCROLL,
_x、 _y,_screenX,_screenY,
假,假,假,假,
假,假,_scrollX,_scrollY,0,0,
_scrollTextXUnits,\u scrollTextX,
_scrollTextYUnits,_scrollTextY,0,null/*PickResult?*/);
Point2D pointOnScene=node.localToScene(node.getLayoutBounds().getWidth()/4,node.getLayoutBounds().getHeight()/4);
最终PickResultChooser结果=新的PickResultChooser();
scene.getRoot().impl_pickNode(新PickRay(pointOnScene.getX(),pointOnScene.getY()),结果);
Node nodeToSendEvent=result.getIntersectedNode();
nodeToSendEvent.fireEvent(scrollEvent);
}