Simple Swing Data Transfer

We begin with an examination of the built-in data transfer capabilities of Swing components and how to perform simple customizations. As a general rule, Swing components that display text and allow it to be selected (JList and JTable, for example, but not JLabel) support Copy and Drag operations. Components that display editable text typically support Cut, Paste, and Drop operations as well. JPasswordField is a special case: it supports Paste and Drop, but because of the sensitivity of password data, does not allow its masked text to be dragged, cut, or copied to the clipboard. Finally, JColorChooser allows colors to be dragged, and JFileChooser allows files to be dragged.

Swing components that support dragging do not enable it by default. To enable drags in a component that supports it, pass true to setDragEnabled( ) . Components that do not support dragging at all do not define this setDragEnabled( ) method.

Swing data transfer is enabled by a TransferHandler object. Components that support data transfer by default have a default TransferHandler installed. The TransferHandler( ) constructor method takes a JavaBeans property name as its sole argument. A TransferHandler created in this way is responsible for transferring the value of the named property. In a Cut, Copy, or Drag operation, it reads the value of the named property and makes it available for transfer. In a Paste or Drop operation, it reads the transferred data and then attempts to set the ...

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.