我正在尝试完成从一个可缩放ScrollPane(“子”)的“拦截”拖动事件,并将该事件重定向到另一个ScrollPane(“父”)。

这是我这样做的尝试:

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application
{
    private final Image PARENT_IMAGE = new Image("http://www.tolkien.co.uk/file/IfbTdA8/5d04a105-e66b-4d9b-b218-928c691eb83d.jpg");
    private final Image CHILD_IMAGE = new Image("http://knightly-slumber.com/worldofwarcraft/files/ScreenShot00019.jpg");

    @Override
    public void start(Stage stage) throws Exception
    {
        stage.setTitle("ScrollPane Sync");

        // Set up the scene (two ScrollPanes, both pannable)
        final ScrollPane parentScrollPane = new ScrollPane();
        final ScrollPane childScrollPane = new ScrollPane();
        parentScrollPane.setPrefSize(600, 400);
        childScrollPane.setPrefSize(200, 200);
        parentScrollPane.setContent(new ImageView(PARENT_IMAGE));
        childScrollPane.setContent(new ImageView(CHILD_IMAGE));
        parentScrollPane.setPannable(true);
        childScrollPane.setPannable(true);
        parentScrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        parentScrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        childScrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        childScrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        parentScrollPane.setCursor(Cursor.MOVE);
        childScrollPane.setCursor(Cursor.MOVE);
        VBox root = new VBox(parentScrollPane, childScrollPane);
        root.setPrefSize(800, 600);

        // Make childScrollPane sync with parentScrollPane's h and v-values
        childScrollPane.hvalueProperty().bind(parentScrollPane.hvalueProperty());
        childScrollPane.vvalueProperty().bind(parentScrollPane.vvalueProperty());

        // Attempt to intercept drag events from childScrollPane and re-direct to parentScrollPane
        childScrollPane.addEventFilter(MouseEvent.MOUSE_DRAGGED, event ->
        {
            // re-map x,y coords relative position from child to parent (as they are different sizes).
            // In other words, if the drag event happened 40% of the way "across" (from the left) and 60%
            // "down" (from the top) the child pane, then re-map those relative positions to absolute
            // positions in terms of the parent pane (40% across and 60% down the parent pane).
            double xRelative = event.getX() / childScrollPane.getViewportBounds().getWidth();
            double xInParent = xRelative * parentScrollPane.getViewportBounds().getWidth();
            double yRelative = event.getY() / childScrollPane.getViewportBounds().getHeight();
            double yInParent = yRelative * parentScrollPane.getViewportBounds().getHeight();

            Point2D screenCoords = parentScrollPane.localToScreen(xInParent, yInParent);

            MouseEvent redirectedEvent = new MouseEvent(
                    parentScrollPane, root,
                    MouseEvent.MOUSE_DRAGGED, xInParent, yInParent,
                    screenCoords.getX(), screenCoords.getY(), event.getButton(), event.getClickCount(),
                    event.isShiftDown(), event.isControlDown(), event.isAltDown(), event.isMetaDown(),
                    event.isPrimaryButtonDown(), event.isMiddleButtonDown(), event.isSecondaryButtonDown(),
                    event.isSynthesized(), event.isPopupTrigger(), event.isStillSincePress(),
                    event.getPickResult()
            );

            MouseEvent.fireEvent(parentScrollPane, redirectedEvent);
            event.consume();
        });

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }


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


我有一个带有两个ScrollPanes的VBox(舞台的根)。两个ScrollPanes都是可缩放的。在这种情况下,我将顶部(较大)的ScrollPane指定为父ScrollPane,将底部(较小的)Scrollpane指定为子ScrollPane。我将子ScrollPane的h和v值绑定到父ScrollPane,以便在父ScrollPane中拖动/平移时,子ScrollPane也移动。然后,我尝试为MouseEvent.MOUSE_DRAGGED事件添加事件过滤器,以便我们可以“拦截”父ScrollPane上的拖动事件。然后,我尝试重定向该事件,并使用父级ScrollPane重新广播该事件,然后触发此新事件,并消耗旧事件。

当前,平移父级ScrollPane会导致子级ScrollPane也移动(这很好),但是在拖动子级ScrollPane时没有任何反应(在这种情况下,我们希望两个ScrollPane都移动)。

一个人可能会问:为什么不对两个ScrollPanes的h和v值仅使用双向绑定?我要避免这样做的原因是它破坏了我要维护的父子关系。换句话说,我希望所有平移都“来自”并由父级ScrollPane控制。

最佳答案

通过这样做,我设法将子事件重定向到父滚动窗格:

// Catch events on scrollPane's children
@FXML
public void press(MouseEvent event) {
    ScrollPane sp = (ScrollPane) ((Node) event.getSource()).getParent().getParent().getParent().getParent();

    PickResult pr = new PickResult(sp.getContent(), event.getSceneX(), event.getSceneY());
    MouseEvent event2 = new MouseEvent(sp, sp.getContent(), event.getEventType(), event.getX(), event.getY(), event.getScreenX(), event.getScreenY(), event.getButton(), event.getClickCount(), event.isShiftDown(), event.isControlDown(), event.isAltDown(), event.isMetaDown(), event.isPrimaryButtonDown(), event.isMiddleButtonDown(), event.isSecondaryButtonDown(), event.isSynthesized(), event.isPopupTrigger(), event.isStillSincePress(), pr);
    sp.getContent().fireEvent(event2);
}

@FXML
public void drag(MouseEvent event) {
    ScrollPane sp = (ScrollPane) ((Node) event.getSource()).getParent().getParent().getParent().getParent();

    PickResult pr = new PickResult(sp.getContent(), event.getSceneX(), event.getSceneY());
    MouseEvent event2 = new MouseEvent(sp, sp.getContent(), event.getEventType(), event.getX(), event.getY(), event.getScreenX(), event.getScreenY(), event.getButton(), event.getClickCount(), event.isShiftDown(), event.isControlDown(), event.isAltDown(), event.isMetaDown(), event.isPrimaryButtonDown(), event.isMiddleButtonDown(), event.isSecondaryButtonDown(), event.isSynthesized(), event.isPopupTrigger(), event.isStillSincePress(), pr);
    sp.getContent().fireEvent(event2);
}

// drag detected => cursor changes
@FXML
public void dragDetected(MouseEvent event) {
    ScrollPane sp = (ScrollPane) ((Node) event.getSource()).getParent().getParent().getParent().getParent();

    PickResult pr = new PickResult(sp.getContent(), event.getSceneX(), event.getSceneY());
    MouseEvent event2 = new MouseEvent(sp, sp.getContent(), event.getEventType(), event.getX(), event.getY(), event.getScreenX(), event.getScreenY(), event.getButton(), event.getClickCount(), event.isShiftDown(), event.isControlDown(), event.isAltDown(), event.isMetaDown(), event.isPrimaryButtonDown(), event.isMiddleButtonDown(), event.isSecondaryButtonDown(), event.isSynthesized(), event.isPopupTrigger(), event.isStillSincePress(), pr);
    sp.getContent().fireEvent(event2);
}


希望它会有所帮助。

关于java - JavaFX-拦截从“子级”可缩放的ScrollPane拖动事件并重定向到“父级” ScrollPane,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29871861/

10-10 08:14