home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Compile DnDTest and run it from the command line (by typing java DnDTest). Open a text editor or other native application that’s 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 drag—the 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 won’t 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:

  dragDropEnd()
  dragEnter()
  dragExit()
  dragOver()
  dropActionChanged()

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 Listeners

Like their AWT counterparts, Swing event listeners are interfaces. Unlike AWT, Sun hasn’t yet implemented adapter classes, so you’ll 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) {
    // don’t 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
  }
}


NOTE:  In Windows jargon, an ancestor is a member of the path of containers that goes back to the root window. If you put a JPanel into a Frame, then put a JLabel on the JPanel, and finally put an Icon in the JLabel, then the JLabel, the JPanel, and the Frame are all ancestors of the Icon.

Understanding Swing Event Sources

Swing events originate in the Swing components. A list showing which events come from which sources follows. Remember the component hierarchy—an event sent by a JComponent is sent by every class that derives from JComponent.

  ActionEvent
AbstractButton
DefaultButtonModel
JDirectoryPane
JTextField
Timer
  AdjustmentEvent
JScrollBar
Spinner
  AncestorEventJComponent
  CellEditorEventDefaultCellEditor
  ChangeEvent
AbstractButton
DefaultBoundedRangeModel
DefaultButtonModel
DefaultCaret
DefaultSingleSelectionModel
FontChooser.Patch
JProgressBar
JSlider
JTabbedPane
JViewport
StandardDialog
StyleContext
  DocumentEventAbstractDocument
  ItemEvent
AbstractButton
DefaultButtonModel
JComboBox
  ListDataEventAbstractListModel
  ListSelectionEvent
DefaultListSelectionModel
JList
  MenuEventJMenu
  PropertyChangeEvent
AbstractAction
DefaultTreeSelectionModel
DirectoryModel
JComponent
TableColumn
  TableColumnModelEventDefaultTableColumnModel
  TableModelEventAbstractTableModel
  TreeExpansionEventDefaultTreeModel
  TreeSelectionEvent
DefaultTreeSelectionModel
JTree
  VetoableChangeEventJComponent
  WindowEventJPopupMenu

Of course, you still have access to events sent by AWT components:

  ComponentEvent—From Component
  FocusEvent—From Component
  KeyEvent—From Component
  MouseEvent—From Component
  MouseMotionEvent—From Component
  ContainerEvent—From Container
  WindowEvent—From Window


Previous Table of Contents Next


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.