- All Implemented Interfaces:
Serializable
At a minimum, users of this class must implement the methods clear(boolean) and addURL(URL). It may be desirable to also implement setFile() addFile(FILE), and setURL(URL) for efficiency. These methods affect the target of a D&D operation and may do more than simply copy a URL and store it.
As an example, suppose a class named InputPane is used to provide
input and can be used as a D&D target when the boolean field dndInput
has the value true
. The class definition of InputPane
could include the following inner class:
This class uses methods of InputPane to store the objects transferred via a D&D operation and additionally overrides importData and canImport to test the the target is the desired one: either an InputPane or a component (e.g., an Icon) of an InputPane. The constructor for InputPane includes the following statements:class FilenameTransferHandler extends ExtObjTransferHandler { @Override protected void clear(boolean all) { InputPane.this.clear(all); } @Override protected void addFile(File f) { InputPane.this.addFile(f); } @Override protected void addURL(URL url) { InputPane.this.addURL(url); } FilenameTransfer(File cdir) { super(cdir); } @Override public boolean importData(TransferHandler.TransferSupport support) { Component comp = support.getComponent(); if (!(comp instanceof InputPane) && !(comp.getParent() instanceof InputPane)) { return false; } return super.importData(support); } @Override public boolean canImport(TransferHandler.TransferSupport support) { Component comp = support.getComponent(); if ((comp instanceof InputPane || comp.getParent() instanceof InputPane)) { if (!dndInput) return false; return super.canImport(support); } return false; } }
fnt = new FilenameTransferHandler(currentDirectory); setTransferHandler(fnt);
The behavior of this class can be modified by calling
setMultiEntryMode(boolean)
or
setMultiEntryMode(boolean,boolean)
.
If not called, the default behavior is to respond to a D&D
request by keeping all existing entries and accepting all the new entries
the D&D operation provides. For some specialized cases, other
options may be desirable, but the default is what one would use in
nearly all cases.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.TransferHandler
TransferHandler.DropLocation, TransferHandler.TransferSupport
-
Field Summary
Fields inherited from class javax.swing.TransferHandler
COPY, COPY_OR_MOVE, LINK, MOVE, NONE
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Transfer an object and add it to a collection of existing objects.protected abstract void
Transfer an object referenced by a URL and add it to the collection of existing objects.boolean
canImport
(TransferHandler.TransferSupport support) protected abstract void
clear
(boolean all) Remove existing objects.boolean
importData
(JComponent comp, Transferable t) boolean
protected void
Transfer an object and clear existing objects.void
setMultiEntryMode
(boolean mode) Set multi-entry mode.void
setMultiEntryMode
(boolean mode, boolean cmode) Set multi-entry mode with the replacement option.protected void
Transfer an object referenced by a URL and clear existing objects.Methods inherited from class javax.swing.TransferHandler
canImport, createTransferable, exportAsDrag, exportDone, exportToClipboard, getCopyAction, getCutAction, getDragImage, getDragImageOffset, getPasteAction, getSourceActions, getVisualRepresentation, setDragImage, setDragImageOffset
-
Constructor Details
-
ExtObjTransferHandler
Constructor.- Parameters:
currentDir
- the current working directory to use for resolving a relative file name referencing an object being transferred.
-
-
Method Details
-
setFile
Transfer an object and clear existing objects. The default implementation converts the argument to a URL passes that URL to setURL.- Parameters:
file
- a file representing the object to be transferred.
-
addFile
Transfer an object and add it to a collection of existing objects. The default implementation converts the argument to a URL passes that URL to addURL.- Parameters:
file
- a file representing the object to be transferred.
-
clear
protected abstract void clear(boolean all) Remove existing objects.- Parameters:
all
- true to clear all entries; false to keep the first entry
-
setURL
Transfer an object referenced by a URL and clear existing objects. The default implementation calls clear() and then addURL(url).- Parameters:
url
- the URL representing the object to be transferred.
-
addURL
Transfer an object referenced by a URL and add it to the collection of existing objects.- Parameters:
url
- the URL representing the object to be transferred.
-
setMultiEntryMode
public void setMultiEntryMode(boolean mode) Set multi-entry mode. Equivalent to setMultiEntryMode(mode, false). When this class is initialized, its state is equivalent to callingsetMultiEntryMode(true)
.- Parameters:
mode
- true if multiple entries are allowed; false otherwise- See Also:
-
setMultiEntryMode
public void setMultiEntryMode(boolean mode, boolean cmode) Set multi-entry mode with the replacement option. The behavior depends on the combination of arguments provided:mode cmode Description true (ignored) A drag & drop operation may insert multiple values, with new entries added to the target false true A drag & drop operation may insert a single value, even if multiple values were selected, and previous values will be cleared from the JList. false false A drag & drop operation may insert multiple values and previous values will be cleared from the JList. - Parameters:
mode
- true if images from multiple D&D entries are kept; false if they are replacedcmode
- true if only one value is to be inserted in a D&D operation when mode has the value false; false if multiple values can be inserted in a D&D operation when mode has the value false; ignored if mode has the value true
-
importData
- Overrides:
importData
in classTransferHandler
-
importData
- Overrides:
importData
in classTransferHandler
-
canImport
- Overrides:
canImport
in classTransferHandler
-