Results 1 to 5 of 5
- 12-08-2009, 12:29 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
drag and drop - disable intra component drop
Hi guys,
I have two JFileChoosers to which I've attached custom TransferHandlers. I'd like to allow the user to drag from one file chooser to the other, but not from one chooser to itself. I can't find anywhere in the api to find the source of a drag, so I'm not sure that this can be done.
I can disallow drops completely, or allow drops in either, but not only from one to the other.
Any suggestions would be greatly appreciated.
Thank you.
- 12-08-2009, 08:14 PM #2
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Suppose both fileChoosers are viewing the same directory; do you want to be able to drag from one to the other??
If so, why prohibit dropping from a fileChooser onto itself?
If not, the code could check whether the origin and destination directories differ.
- 12-08-2009, 09:59 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
The two file choosers will be looking at different file systems. One of them the local drive, and one of them a remote drive, like an ftp client. So there isn't a chance of them looking at the same directory.
Ideally I'd like to know in the canImport method if the drop is allowed so the cursor can accurately represent the allowed actions.Last edited by tomba; 12-08-2009 at 10:13 PM.
- 12-09-2009, 01:52 AM #4
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
(Caveat: I've never written a drag-n-drop app.)
Can you create two new DataFlavor objects and associate one with data from the first file choosers and the other with the second?
Here's a JFileChooser subclass with an addDataFlavor method:
}Java Code:package Test; import java.io.*; import javax.swing.*; import java.awt.datatransfer.*; import javax.swing.filechooser.FileSystemView; /** * A JFileChooser to which additional DataFlavor values can be added. * Such values might be used in identifying * which JFileChooser is the source of a Drag-n-Drop. * * @author zweibieren * * a JComponent implements getTransferHandler * a TransferHandler implements createTransferable * a Transferable implements getDataFlavors * so to add a data flavor we have to subclass TransferHandler and Transferable */ public class FlavorfulFileChooser extends JFileChooser { DataFlavor [] flavors; public FlavorfulFileChooser(File currentDirectory, FileSystemView fsv) { super(currentDirectory, fsv); FFTransferHandler newTH = new FFTransferHandler(); // get initial flavors list Transferable baseT = newTH.createTransferable(this); flavors = baseT.getTransferDataFlavors(); if (flavors == null) flavors = new DataFlavor[0]; setTransferHandler(newTH); } public void addDataFlavor (DataFlavor newFlav) { int n = flavors.length; DataFlavor [] old = flavors; flavors = new DataFlavor[n + 1]; System.arraycopy(old, 0, flavors, 0, n); flavors[n] = newFlav; } class FFTransferHandler extends TransferHandler { public FFTransferHandler () { super(); } @Override public Transferable createTransferable(JComponent c) { return new FFTransferable(super.createTransferable(c)); } @Override public int getSourceActions(JComponent c) { return COPY; } } class FFTransferable implements Transferable { Transferable baseT; FFTransferable(Transferable base) { baseT = base; } public DataFlavor[] getTransferDataFlavors() { return flavors; } public boolean isDataFlavorSupported(DataFlavor flavor) { // unwarranted assumption: uses getTransferDataFlavors // return baseT.isDataFlavorSupported(flavor); for (DataFlavor df : flavors) if (df.equals(flavor)) return true; return false; } public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { return baseT.getTransferData(flavor); } } void printDebugInfo(String msg) { System.out.println(); System.out.println(msg); FFTransferHandler th = (FFTransferHandler) getTransferHandler(); System.out.println("TransferHandler is class " + th.getClass().getCanonicalName()); Transferable t = th.createTransferable(this); System.out.println("Transferable is class " + t.getClass().getCanonicalName()); DataFlavor [] flavs = t.getTransferDataFlavors(); System.out.print(" flavors:"); for (DataFlavor df : flavs) System.out.print(" " + df.getHumanPresentableName()); System.out.println(); } /** * @param args the command line arguments */ public static void main(String[] args) { FlavorfulFileChooser fffc = new FlavorfulFileChooser(new File("."), FileSystemView.getFileSystemView()); fffc.printDebugInfo("initial"); fffc.addDataFlavor(new DataFlavor(File.class, "tag")); fffc.printDebugInfo("after adding flavor tag"); fffc.addDataFlavor(new DataFlavor(File.class, "tagtoo")); fffc.printDebugInfo("after adding flavor tagtoo"); } }
- 12-09-2009, 01:01 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Drag and Drop
By carderne in forum New To JavaReplies: 0Last Post: 08-31-2009, 09:18 AM -
Drag and Drop(URGENT)
By simmi in forum AWT / SwingReplies: 5Last Post: 07-10-2009, 11:16 AM -
Drag and drop
By thayalan in forum AWT / SwingReplies: 1Last Post: 02-16-2009, 03:04 PM -
Drag and drop
By abhivenugopal in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01-30-2008, 02:10 PM -
Is it possible to disable drop in a component?
By Ada in forum AWT / SwingReplies: 3Last Post: 06-07-2007, 05:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks