如何在JavaFX Windows外部拖动JavaFX节点并检测放置事件? [英] How to drag a JavaFX node and detect a drop event outside the JavaFX Windows?

查看:87
本文介绍了如何在JavaFX Windows外部拖动JavaFX节点并检测放置事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个可以从JavaFX窗口分离的TabPane.将选项卡窗格拖到其所在的窗口之外时,我希望创建一个新窗口并将该选项卡放置在该窗口中.

I'm implementing a TabPane that can be detached from the JavaFX window. When the tab pane is dragged outside of the window where it came from, I would like a new window to be created and the tab to be placed in that window.

我已经实现了一些使用拖动手势在现有窗口之间拖动选项卡的方法.但是,当鼠标移到JavaFX场景之外时,我无法接收任何鼠标事件或拖动事件.这有可能吗?

I already implemented some methods using drag gestures to drag the tab between existing windows. However, I am not able to receive any mouse events or drag events when the mouse is moved outside of JavaFX scenes. Is this something possible?

推荐答案

您可以在相应的节点上监听鼠标释放的事件(例如,选项卡的图形).使用MouseEvent.getScreenX()MouseEvent.getScreenY()检查屏幕坐标,并查看它们是否位于当前窗口之外.如果是这样,请创建一个新的窗口,场景和选项卡窗格;否则,请单击确定".从当前选项卡窗格中删除该选项卡,并将其放置在新的选项卡中.

You can listen for a mouse released event on the appropriate node (e.g. the tab's graphic). Check the screen coordinates using MouseEvent.getScreenX() and MouseEvent.getScreenY() and see if they lie outside the current window. If they do, create a new window, scene, and tab pane; remove the tab from the current tab pane and place it in the new one.

这是一个基本的示例,没有多余的装饰(例如,没有用户提示正在发生拖动的事实),但是它将为您提供一个主意:

Here's a basic example with no frills (e.g. no user hints as to the fact that dragging is occuring), but it will give you the idea:

import javafx.application.Application;
import javafx.collections.ListChangeListener.Change;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.Window;

public class DetachableTabExample extends Application {

    private int tabCount = 0 ;

    @Override
    public void start(Stage primaryStage) {
        TabPane tabPane = new TabPane();
        Button newTabButton = new Button("New Tab");
        newTabButton.setOnAction(event -> {
            Tab tab = new Tab();
            Label tabLabel = new Label("Tab "+(++tabCount));
            tab.setGraphic(tabLabel);
            tab.setContent(new TextArea());
            tabPane.getTabs().add(tab);

            tabLabel.setOnMouseReleased(me -> {
                Point2D mouseLoc = new Point2D(me.getScreenX(), me.getScreenY());
                Window window = tabPane.getScene().getWindow();
                Rectangle2D windowBounds 
                    = new Rectangle2D(window.getX(), window.getY(),
                                      window.getWidth(), window.getHeight());
                if (! windowBounds.contains(mouseLoc)) {
                    tabPane.getTabs().remove(tab);
                    Stage newStage = new Stage();
                    TabPane newTabPane = new TabPane();
                    newTabPane.getTabs().add(tab);
                    Scene scene = new Scene(new BorderPane(newTabPane));
                    newStage.setScene(scene);
                    newStage.setX(me.getScreenX());
                    newStage.setY(me.getScreenY());
                    newStage.setWidth(window.getWidth());
                    newStage.setHeight(window.getHeight());
                    newStage.show();
                }
            });

        });


        BorderPane root = new BorderPane(tabPane, newTabButton, null, null, null);
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

这篇关于如何在JavaFX Windows外部拖动JavaFX节点并检测放置事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆