简体   繁体   中英

Java drag and drop doesnt work

I'm trying Java drag and drop action. Action works by mobilizing the path to the image that I wanted to portray subsequently label, but are not plotted, can someone please help?

class MyDragDropListener implements DropTargetListener {

    public void drop(DropTargetDropEvent event) {

        // Accept copy drops
        event.acceptDrop(DnDConstants.ACTION_COPY);

        // Get the transfer which can provide the dropped item data
        Transferable transferable = event.getTransferable();

        // Get the data formats of the dropped item
        DataFlavor[] flavors = transferable.getTransferDataFlavors();

        // Loop through the flavors
        for (DataFlavor flavor : flavors) {

            try {

                // If the drop items are files
                if (flavor.isFlavorJavaFileListType()) {


                    //       List files = (List) transferable.getTransferData(flavor);

                    Object f = transferable.getTransferData(flavor);
                    // Loop them through
                    //   for (File file : files) {

                    // Print out the file path
//                        System.out.println("File path is '" + f.getPath() + "'.");

                    String cesta = f.toString();
                    String typ = "";
                    String cesta2 = "";
                    for (int i = 1; i < cesta.length() - 1; i++) {
                        if (cesta.charAt(i) == '\\') {
                            cesta2 = cesta2 + '/';
                        } else {
                            cesta2 = cesta2 + cesta.charAt(i);
                        }
                    }
                    for (int i = cesta2.length() - 1; i > 0; i--) {
                        if (cesta2.charAt(i) == '.') {
                            break;
                        }
                        typ = typ + cesta2.charAt(i);

                    }
                    typ = new StringBuilder(typ).reverse().toString().toLowerCase();


                    MojeOkno mo = new MojeOkno();
                    // Inform that the drop is complete
                    mo.VlozObrazekDoLabelu(cesta2);
                    System.out.println("METODA PROBEHLA");

                }

            } catch (Exception e) {

                // Print out the error stack
                e.printStackTrace();

            }
        }

        event.dropComplete(true);

    }

    public void dragEnter(DropTargetDragEvent event) {
    }

    public void dragExit(DropTargetEvent event) {
    }

    public void dragOver(DropTargetDragEvent event) {
    }

    public void dropActionChanged(DropTargetDragEvent event) {
    }
}



 public void VlozObrazekDoLabelu(String Mojecesta) {

        System.out.println("Cesta k obrazku: " + Mojecesta);

        try {

            img = ImageIO.read(new File(Mojecesta));


            Graphics2D g = (Graphics2D) PanelProObrazek_jPanel1.getGraphics();
            g.drawImage(img, 3, 3, PanelProObrazek_jPanel1.getWidth() - 6, PanelProObrazek_jPanel1.getHeight() - 6, this);
            System.out.println("Dostal jsem se do try");

        } catch (Exception e) {
            System.out.println("CHYBA");

        }

    }

Greetings from Czech Republic.

The problem is in MyDragDropListener.drop(DropTargetDropEvent event)

// CESTA2 == PATH TO IMG
MojeOkno mo = new MojeOkno();
mo.InserImgToPanel(cesta2);

You created a new MojeOkno, that's why it wont work. Make several changes:

// 1. method Callmeafterstart
MyDragDropListener myDragDropListener = new MyDragDropListener(this);

// 2. class MyDragDropListener
private MojeOkno mojeOkno;
public MyDragDropListener(MojeOkno mojeOkno) {
    this.mojeOkno = mojeOkno;
}

// 3. method drop
// CESTA2 == PATH TO IMG
mojeOkno.InserImgToPanel(cesta2);

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