javafx实现自定义的数据拖拽

效果

请添加图片描述

代码

package cn.juhe.zjsb.test;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.Serializable;


public class DragAndDropPersonExample extends Application {
    public DataFormat dataFormat = new DataFormat("data/person");
    VBox getVBox() {
        VBox vBox = new VBox(10);
        vBox.setPrefWidth(300);
        vBox.setPrefHeight(400);
        vBox.setStyle("-fx-border-color: #ff0000");
        return vBox;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        Scene scene = new Scene(anchorPane, 900, 900);
        VBox vBox = getVBox();
        VBox vBoxView = getVBox();
        Person person = new Person("小倩老师", "18", "file:/Users/java0904/zhengjianshibie/src/main/resources/cn/juhe/zjsb/img/laoshi.jpeg");
        Button infoBtn = new Button("个人详情");
        infoBtn.prefWidthProperty().bind(vBox.widthProperty());
        TextField nameField = new TextField();
        TextField ageField = new TextField();
        ImageView imageView = new ImageView();
        TextField nameFieldView = new TextField(person.getName());
        TextField ageFieldView = new TextField(person.getAge());
        ImageView imageViewView = new ImageView(new Image(person.getPhoto()));
        imageView.setPreserveRatio(true);
        imageView.setFitWidth(300);
        imageViewView.setPreserveRatio(true);
        imageViewView.setFitWidth(300);
        vBoxView.getChildren().addAll(nameFieldView,ageFieldView,imageViewView);
        vBox.getChildren().addAll(infoBtn,nameField,ageField,imageView);
        Button personBtn = new Button(person.getName());
        personBtn.setOnDragDetected(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                //获取拖动板
                Dragboard dragboard = personBtn.startDragAndDrop(TransferMode.MOVE);
                //创建剪切板
                ClipboardContent clipboardContent = new ClipboardContent();
                //设置剪切板的数据格式与数据,dataFormat是自己定义的,data/person是随便写的
                clipboardContent.put(dataFormat,person);
                anchorPane.getChildren().add(vBoxView);
                WritableImage writableImage = new WritableImage(200,300);
                vBoxView.snapshot(new SnapshotParameters(),writableImage);
                anchorPane.getChildren().remove(vBoxView);
                dragboard.setDragView(writableImage);
                //设置拖动板的内容
                dragboard.setContent(clipboardContent);
            }
        });
        vBox.setOnDragOver(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent dragEvent) {
                dragEvent.acceptTransferModes(TransferMode.MOVE);
                Object content = dragEvent.getDragboard().getContent(dataFormat);
                Person p = (Person) content;
                nameField.setText(p.getName());
                ageField.setText(p.getAge());
                System.out.println(p.getPhoto());
                imageView.setImage(new Image(p.getPhoto()));
            }
        });
        anchorPane.getChildren().addAll(personBtn, vBox);
        AnchorPane.setLeftAnchor(personBtn, 100.0);
        AnchorPane.setTopAnchor(personBtn, 100.0);
        AnchorPane.setLeftAnchor(vBox, 300.0);
        AnchorPane.setTopAnchor(vBox, 200.0);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Drag and Drop Image Example");
        primaryStage.show();

    }

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


class Person implements Serializable {
    String name;
    String age;
    String photo;

    public Person(String name, String age, String photo) {
        this.age = age;
        this.name = name;
        this.photo = photo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

micro_cloud_fly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值