Exercises
Exercise 14-1. The
DigitalClock
class of Example 14-2 uses aMouseMotionListener
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 aMouseListener
in addition to theMouseMotionListener
.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 aTransferHandler
to transfer the value of the “color” property. Next, modifyColorSwatch
to add a property named “allowsDrop”. If this property isfalse
, then theColorSwatch
should not allow colors to be dropped onto itself. To do this, you’ll need to subclassTransferHandler
to modify thecanImport( )
andimportData( )
methods.Exercise 14-3. The
copy( )
method of Example 14-5 defines an anonymous implementation of theClipboardOwner
interface that has an emptylostOwnership( )
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 ...
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.