Minggu, 21 September 2014

Tutorial Drag and Drop di java



Sudah sangat umum fitur drag and drop disematkan kedalam sebuah aplikasi sehingga menyingkat suatu proses, pada java fitur drag and drop (DnD) juga bisa diterapkan, seperti berikut yaitu menampilkan gambar pada Jbutton dengan melakukan DnD sebuah file gambar






Mekanisme nya adalah komponen disematkan method setDrop(new DropTarget())
Letakan code berikut pada constructor Jframe





        button1.setDropTarget(new DropTarget() {
            @Override
            public void drop(DropTargetDropEvent dtde) {
                button1DnD(dtde);
            }
        });


       
 



Kemudian kita membuat method yang akan didaftarkan ke method drop


    private void button1DnD(DropTargetDropEvent dtde)
    {
            try {
                    Transferable transfer = dtde.getTransferable();
                    if(transfer.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                        List objects = (List)transfer.getTransferData(DataFlavor.javaFileListFlavor);
                        for(Object object : objects) {
                            if(object instanceof File) {
                                File source = (File)object;
                                File dest = new File(System.getProperty("user.home")+File.separator+source.getName());
                                Files.copy(Paths.get(source.getAbsolutePath()), Paths.get(dest.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
                                //System.out.println("File copied from "+source.getAbsolutePath()+" to "+dest.getAbsolutePath());
                                button1.setText("");
                                button1.setIcon(new ImageIcon(ImageIO.read(new File(dest.getAbsolutePath()))));
                                int tinggi = ImageIO.read(new File(dest.getAbsolutePath())).getHeight();
                                int lebar = ImageIO.read(new File(dest.getAbsolutePath())).getWidth();
                                jLabel1.setText("Tinggi :"+tinggi+" Lebar :"+lebar+" Tipe : "+ImageIO.read(new File(dest.getAbsolutePath())).getType());
                            }else
                            {
                                button1.setText(pesan);
                            }
                        }
                    } else if(transfer.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                        String type = (String)transfer.getTransferData(DataFlavor.stringFlavor);
                        //System.err.println("Data flavor not supported: "+type);
                    } else {
                       // System.err.println("Data flavor not supported.");
                    }
                } catch(UnsupportedFlavorException ex) {
                    //System.err.println(ex.getMessage());
                } catch(IOException ex) {
                   // System.err.println(ex.getMessage());
                } catch(Exception ex) {
                    //System.err.println(ex.getMessage());
                } finally {
                    dtde.dropComplete(true);
                }
    }


       
 

Download
https://www.dropbox.com/s/964k3nyel6qxzbt/Release%20Aplikasi%20Viewer%20Drag%20and%20drop.rar?dl=0
Posting Komentar