[ Team LiB ] Previous Section Next Section

14.7 Exercises

Exercise 14-1. The DigitalClock class of Example 14-2 uses a MouseMotionListener to initiate a drag when the user drags the mouse. It initiates a drag even when the user meant to click but jiggled the mouse a pixel or two. Modify the example so that a drag is not initiated unless the mouse has actually moved four pixels from where the button was pressed down. You'll need a MouseListener in addition to the MouseMotionListener.

Exercise 14-2. Define a custom Swing component, named ColorSwatch, with a read/write property named "color" that displays a color swatch and allows that color to be dragged. Use a TransferHandler to transfer the value of the "color" property. Next, modify ColorSwatch to add a property named "allowsDrop". If this property is false, then the ColorSwatch should not allow colors to be dropped onto itself. To do this, you'll need to subclass TransferHandler to modify the canImport( ) and importData( ) methods.

Exercise 14-3. The copy( ) method of Example 14-5 defines an anonymous implementation of the ClipboardOwner interface that has an empty lostOwnership( ) method. Provide an implementation of this method to deselect the currently selected line: this provides visual feedback to the user that indicates the line is no longer available for pasting. Test your method by selecting and copying a line in one instance of the component, and then doing the same thing in another instance. Next, modify your method so that it only deselects the selected line if it is the same one that was transferred to the clipboard.

    [ Team LiB ] Previous Section Next Section