|
Drag and Drop: New Data Transfer Capabilities in the JavaTM 2 Platform, Standard Edition (J2SETM), version 1.4(14) As long as you associate the earlier ImageSelection handler as the TransferHandler for the component, you can drag off the image to a native application that accepts image input. It is that easy. The ImageSelection handler is a little special in the sense that it doesn't transfer a property of the button/label. Instead it gets the Image from the Icon property of the component. When the transferable object actually is a property of the component, the creation of the handler can be done mUCh more easily. The TransferHandler class has a constrUCtor that accepts a property name as the constrUCtor argument. Then, when you associate the handler with a Swing component, it knows how to acquire the appropriate content. For instance, if you wanted to support the dragging of the text label for a JLabel, the following would work to create the TransferHandler for the label and associate it with the component: TransferHandler handler = new TransferHandler("text");JLabel label = new JLabel("Welcome");label.setTransferHandler(handler);You would still need to add the MouseListener to the component, but you don't have to do anything special to create the handler for the component. If a Swing component doesn't provide built in drop support, you have to add that in yourself. The code to get the data being transferred via the drag-and-drop operation is identical to getting the data from the clipboard. You only need to associate the behavior with some triggering action, like when the mouse is released over a component: MouseListener releaseListener = new MouseAdapter() {public void mouseReleased(MouseEvent e) { Transferable clipData = clipboard.getContents(clipboard); if (clipData != null) {if (clipData.isDataFlavorSupported(DataFlavor.imageFlavor)) {TransferHandler handler =component.getTransferHandler();handler.importData(component, clipData);} }}};
|