Java Swing Drag and DROP source

zhaozj2021-02-08  232

Import java.aw. *; import java.util. *; import java.awt.event. *; import java.awt.dnd. *; import java.awt.datatransfer. *; Import Javax .swing. *; import javax.swing.event. *; import javax.swing.tree. *;

Public Class Listdemo Extends Jframe Implements ListSelectionListener {Private DroppableList List; Private JtextField FileName

Public ListDemo () {Super ("ListDemo");

// Create the list and put it in a scroll pane list = new DroppableList (); DefaultListModel listModel = (DefaultListModel) list.getModel (); list.setCellRenderer (new CustomCellRenderer ()); list.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); List.setselectedIndex (0); List.AddListSelectionListener (this); JscrollPane ListScrollPane = New JscrollPane (List);

String Dirname = new string ("//"); string filelist [] = new file (dirname) .list (); for (int i = 0; i

Filename = new jtextfield (50); string name = listModel.getlementat (list.getSelectedIndex ()). Tostring (); filename.settext (name);

file:. // Create a panel that uses FlowLayout (the default) JPanel buttonPane = new JPanel (); buttonPane.add (fileName); Container contentPane = getContentPane (); contentPane.add (listScrollPane, BorderLayout.CENTER); contentPane. Add (ButtonPane, BorderLayout.North);

Public void valuechanged (ListSelectionEvent E) {if (E.GetValueisadjusting () == false) {filename.setText (""); if (list.getSelectedIndex ()! = -1) {String name = list.getSelectedValue (). Tostring (); filename.settext (name);}}}

Private static hashtable manetode (String Name, String Url, String String) {HashTable Hashtable = new hashtable (); hashtable.put ("name", name); hashtable.put ("URL", URL); Hashtable.Put (" Path ", strpath); return hashtable;}

public class DroppableList extends JList implements DropTargetListener, DragSourceListener, DragGestureListener {DropTarget dropTarget = new DropTarget (this, this); DragSource dragSource = DragSource.getDefaultDragSource ();

Public DropPableList () {Dragsource.createDefaultDraggeStureRecognizer (this, dndconstants.action_copy_or_move, this); setmodel (new defaultListModel ());}

public void dragDropEnd (DragSourceDropEvent DragSourceDropEvent) {} public void dragEnter (DragSourceDragEvent DragSourceDragEvent) {} public void dragExit (DragSourceEvent DragSourceEvent) {} public void dragOver (DragSourceDragEvent DragSourceDragEvent) {} public void dropActionChanged (DragSourceDragEvent DragSourceDragEvent) {} public void dragEnter (DropTargetDragEvent DROPTARGETDRAGEVENT) {DROPTARGETDRAGEVENT.ACCEPTDRAG (DNDCONSTANTS.ACTION_COPY_OR_MOVE);}

public void dragExit (DropTargetEvent dropTargetEvent) {} public void dragOver (DropTargetDragEvent dropTargetDragEvent) {} public void dropActionChanged (DropTargetDragEvent dropTargetDragEvent) {}

public synchronized void drop (DropTargetDropEvent dropTargetDropEvent) {try {Transferable tr = dropTargetDropEvent.getTransferable (); if (tr.isDataFlavorSupported (DataFlavor.javaFileListFlavor)) {dropTargetDropEvent.acceptDrop (DnDConstants.ACTION_COPY_OR_MOVE); java.util.List fileList = (java .util.List) tr.getTransferData (DataFlavor.javaFileListFlavor); Iterator iterator = fileList.iterator (); while (iterator.hasNext ()) {File file = (File) iterator.next (); Hashtable hashtable = new Hashtable ( ); Hashtable.Put ("name", file.getname ()); hashtable.put ("URL", file.tourl (). Tostring ()); Hashtable.Put ("Path", file.getabsolutePath ()) ()) (DefaultListModel) Get . Model ()) addElement (hashtable);} dropTargetDropEvent.getDropTargetContext () dropComplete (true);.} Else {System.err.println ( "Rejected"); dropTargetDropEvent.rejectDrop ();}} catch (IOException io) { IO.PrintStackTrace (); DROPTARGETDROPEVENT.REJECTDROP ();} catch (unsupportedflavorexception ufe) {ufe.printstacktrace (); DROPTARGETDROPEVENT.REJECTDROP ();}}

public void dragGestureRecognized (DragGestureEvent dragGestureEvent) {if (getSelectedIndex () == -1) return; Object obj = getSelectedValue (); if (obj == null) {// Nothing selected, nothing to drag System.out.println ( " Nothing selected - beep "); getToolkit (.) beep ();} else {Hashtable table = (Hashtable) obj; FileSelection transferable = new FileSelection (new File ((String) table.get (" path "))); dragGestureEvent .startdrag (Dragsource.defaultcopydrop, Transferable, this);}}}

public class CustomCellRenderer implements ListCellRenderer {DefaultListCellRenderer listCellRenderer = new DefaultListCellRenderer (); public Component getListCellRendererComponent (JList list, Object value, int index, boolean selected, boolean hasFocus) {listCellRenderer.getListCellRendererComponent (list, value, index, selected, hasFocus); listCellRenderer .setText (getValueString (value)); return listCellRenderer;} private String getValueString (Object value) {String returnString = "null"; if (! value = null) {if (value instanceof Hashtable) {Hashtable h = (Hashtable) value String name = (string) h.get ("name"); string url = (string) h.Get ("URL"); returnstring = name "==>" URL;} else {returbtring = "x " value.toString (); }}} Returnstring;}}

public class FileSelection extends Vector implements Transferable {final static int FILE = 0; final static int STRING = 1; final static int PLAIN = 2; DataFlavor flavors [] = {DataFlavor.javaFileListFlavor, DataFlavor.stringFlavor, DataFlavor.plainTextFlavor}; public FileSelection (File file) {addElement (file);}. / * Returns the array of flavors in which it can provide the data * / public synchronized DataFlavor [] getTransferDataFlavors () {return flavors;} / * Returns whether the requested flavor is supported By this object. * / public boolean isdataflavorsupported (DataFlavor flavor) {boolean b = false; b | = flavor.equals (flavors [file]); b | = flavor.equals (flavors [string]); b | = flavor. Equals (Flavors [plain]); return (b);} / ** * if the data was request ed in the "java.lang.String" flavor, * return the String representing the selection. * / public synchronized Object getTransferData (DataFlavor flavor) throws UnsupportedFlavorException, IOException {if (flavor.equals (flavors [FILE])) {return this Else IF (Flavor.Equals (Flavors [Plain])) {Return New StringReader ((File) Elementat (0)). GetabsolutePath ());} else if (Flavor.equals (Flavors [string])) { Return ((file) Elementat (0)). GetabsolutePath ();} else {throw new unsupportedflavorException (flavor);}}}

转载请注明原文地址:https://www.9cbs.com/read-2990.html

New Post(0)