【发布时间】:2015-04-25 23:25:19
【问题描述】:
我正在尝试从一个可平移的 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 控制。
【问题讨论】:
标签: java javafx mouseevent javafx-8