|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Compile DnDTest and run it from the command line (by typing java DnDTest). Open a text editor or other native application thats DnD-capable and drag some text onto the Java application. Your Java program detects the drag event over the DropTarget (called a DropTargetDragEvent) and calls dragEnter(). Because the incoming data is plain text, dragEnter() accepts the dragthe user sees the default cursor showing a drag-copy in progress. If the user moves the cursor out of the target area, the program calls dragExit(). If the user drops the transferred object over the target, your program calls drop(). In this case, DnDTest extracts the plain text from the transferred flavor and places it in the TextArea. Your Java program can be a source for a drag-and-drop operation as well as being a target. Your source should implement DragGestureListener so that Java recognizes a click-and-drag action as the start of a drag-and-drop. The source should also implement DragSourceListener, though you wont need its methods unless you want a special effect. DragGestureListener requires that you implement only one method: dragGestureRecognized(). In this method, you should determine what data is to be transferred, then package that data into a flavor. If your interface stores data in a Vector called fModel, for example, and the user has selected theItem, you might write: Transferable theTransferable = null; theTransferable = (Transferable) fModel.elementAt(theItem); DataFlavor theFlavors[] = theTransferable.getTransferDataFlavors(); try { theDragGestureEvent.startDrag(DragSource.DefaultCopyDrop, theTransferable, this); } catch (InvalidDnDOperationException e) { System.out.println(Invalid Drag and Drop operation: + e); } DragSourceListener requires that you implement five methods:
Unless you need a special effect, you can stub out all these methods. Now when you start a drag, dragGestureRecognized() runs, stuffs the data into one or more flavors, and attaches the array of flavors to the DragGestureEvent. When the cursor enters a drop target, your program examines the array of flavors; if it finds one that it can accept, it accepts the drag. Finally, when the user drops the transferred object, your program reads out the data from the flavor and puts it to work in the target. Using Swing Event ListenersLike their AWT counterparts, Swing event listeners are interfaces. Unlike AWT, Sun hasnt yet implemented adapter classes, so youll need to override every listener method in order to implement a listener. To implement an AncestorListener (which has three methods), for example, you might write public class myAncestorListener implements AncestorListener { public void ancestorAdded(AncestorEvent e) { // ignore this one } public void ancestorRemoved(AncestorEvent e) { // dont care about this one either } public void ancestorMoved(AncestorEvent e) { // do something in this case Here is code to handle the case of a moving ancestor } }
Understanding Swing Event SourcesSwing events originate in the Swing components. A list showing which events come from which sources follows. Remember the component hierarchyan event sent by a JComponent is sent by every class that derives from JComponent.
Of course, you still have access to events sent by AWT components:
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |