|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Understanding Swing EventsEarlier in this chapter, in Using the Abstract Windowing Toolkit, we introduced the delegation event model and talked about low-level and semantic events. Swing has its own event package for Swing-specific events. Use the javax.swing.event package for the event listeners and the events themselves; the event sources are the Swing components. The following is a list of the Swing event classesDocumentEvent is an interfaceand their meanings:
class TMouseAdapter extends java.awt.event.MouseAdapter { public synchronized void mousePressed( ⇒java.awt.event.MouseEvent anEvent) { anEvent.consume(); fThreadSuspended = !fThreadSuspended; if (!fThreadSuspended) synchronized (fMonitorThread) { fMonitorThread.notifyAll(); } } } To learn more about TRaceApplet.java, see Sample Applets on p. 1001. Drag and DropIn the latest version of the JDK, all drag-and-drop (DnD) operations (for both Swing and AWT components) are supported through the interface java.awt.dnd. Listing 38.14 shows a typical DnD program.
Listing 38.14 DnDTest.javaAn Implementation of a DropTargetListener import java.awt.*; import java.awt.dnd.*; import java.awt.datatransfer.*; import java.awt.event.*; import java.io.*; public class DnDTest extends Frame implements DropTargetListener { DropTarget fTarget; TextArea fTextArea; public DnDTest () { fTextArea = new TextArea(Grab some text and \npull it over here!); fTarget = new DropTarget(text, DnDConstants.ACTION_COPY, this); fTarget.setActive (true); add (Center, fTextArea); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } public static void main (String args[]) { DnDTest theFrame = new DnDTest(); theFrame.setSize (400, 200); theFrame.setVisible (true); } // required methods in DropTargetListener public void dragEnter (DropTargetDragEvent dtde) { System.out.println (dragEnter); // use the DropTargetDragEvent to find out what flavors are offered DataFlavor df[] = dtde.getCurrentDataFlavors(); for (int i = 0; i < df.length; i++) { if (df[i].equals (DataFlavor.plainTextFlavor)) { // if the flavor is plain text, accept the drag dtde.acceptDrag (DnDConstants.ACTION_COPY); return; } } // otherwise reject this drag dtde.rejectDrag (); } public void dragOver (DropTargetDragEvent dtde) { // dragOver is called repeatedly while the cursor is over // the target. Uncomment the println to see how often this // method is called. //System.out.println (dragOver); } public void dropActionChanged(DropTargetDragEvent dtde) { System.out.println(dropActionChanged); } public void dragScroll (DropTargetDragEvent dtde) { System.out.println (dragScroll); } public void dragExit (DropTargetEvent dte) { System.out.println (dragExit); } public void drop (DropTargetDropEvent dtde) { dtde.acceptDrop (DnDConstants.ACTION_COPY); System.out.println (dropped); Transferable theTransferable = dtde.getTransferable(); DataFlavor df[] = dtde.getCurrentDataFlavors(); Object theObject = null; try { // search through the flavor looking for plain text // when you find it get the associated data and stuff // it into the Object. for (int i = 0; i < df.length; i++) { if (df[i].equals (DataFlavor.plainTextFlavor)) { theObject = theTransferable.getTransferData(df[i]); } } // if the data comes from outside the JVM, it comes in // on an InputStream. Read in the stream. if (theObject != null && theObject instanceof InputStream) { InputStream theInputStream = (InputStream) theObject; StringBuffer theStringBuffer = new StringBuffer(); byte[] aBuffer = new byte[64]; int theCount = theInputStream.read(aBuffer); while (theCount != -1) { // keep stuffing data into the string buffer theStringBuffer.append (new String (aBuffer, 0, theCount)); theCount = theInputStream.read(aBuffer); } // Weve read in all the data. Now lets use it theInputStream.close(); fTextArea.setText (theStringBuffer.toString()); } } catch (Exception e) { e.printStackTrace(); } finally { try { fTarget.getDropTargetContext().dropComplete(true); } catch (Exception ignore) {} } } // lets handle a click on the windows close box protected void processWindowEvent (WindowEvent e) { if (e.getID() == WindowEvent.WINDOW_CLOSING) System.exit(0); super.processWindowEvent (e); } }
|
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. |