简体   繁体   中英

Use drag and drop cursor java swing or javafx

I would like to change the cursor to use the drag and drop "move" and "move and copy" cursors in my application on a panel when I perform certain actions. The area is using swing inside of a swing node so a swing (Apply to panel) or JavaFx (Applying to swing node) solution would work.

I did find swing cursors DragSource.DefaultMoveDrop and DragSource.DefaultCopyDrop but when I apply them my cursor doesn't change. (And I know I am applying the cursor correctly as applying other cursors work fine)

EDIT: Note that when not nested in a SwingNode and in a JFrame instead the cursor does change to what I want. I have amended the code example to emulate the environment I am in where it doesn't work:

public class Temp extends Application {
    
    public static void main(String[] args) {
        Application.launch();
    }
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        SwingNode swingNode = new SwingNode();
        SwingUtilities.invokeLater(() -> {
            JPanel panel = new JPanel();
            panel.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); //Change this
            swingNode.setContent(panel);
        });
        Scene scene = new Scene(new BorderPane(swingNode));
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
}

Ok turns out it works DragSource.DefaultMoveDrop and DragSource.DefaultCopyDrop actually work in a normal swing application, just not on JPanel inside a SwingNode which the environment I am working in uses. (I can't change this)

Therefore if you run into this issue and you are NOT mixing the frameworks this forms an answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM