2013-05-02 112 views
0

我有一個監聽滾輪鼠標滾動的窗格;以及我有一個滾動條,它會自動偵聽滾輪鼠標滾動。我想知道如何發送滾動窗格捕獲的窗格事件到滾動條。如何將事件從一個節點發送到另一個節點

我不能使用滾動窗格,因爲我需要一個窗格的自定義實現,我已經嘗試使用滾動窗格,它沒有覆蓋我的需要。

我試圖觸發事件和其他方法,但我只能將事件傳遞/傳播/發送到滾動條。

在此先感謝

示例應用程序:

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); 
    } 

} 

回答

1

看來,你的問題是等於需要自定義滾動窗格中實施。我認爲,一種可能的方式 - 解析事件,並使事件發生時滾動條更改其值(事件發生時添加偵聽器,解析向上或向下滾動事件,並使用方法遞增或遞​​減滾動條)。

此外,我們使用一些實現滾動事件發送。使用此劃痕實施:

/** 
* 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); 
} 
相關問題