Java/JDK 6/Drag and Drop
Содержание
- 1 Drag and drop between JList and JTextField
- 2 Drag and drop between JTextArea and JTextField
- 3 DropMode.INSERT
- 4 DropMode.ON
- 5 DropMode.ON_OR_INSERT
- 6 Set tree drag mode to DropMode.INSERT
- 7 Set tree drag mode to DropMode.ON
- 8 Set tree drag mode to DropMode.ON_OR_INSERT
- 9 Set tree DropMode to DropMode.USE_SELECTION
- 10 Transfer both Text and Color between JTextField and JTextArea
Drag and drop between JList and JTextField
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.TransferHandler;
public class DnDDemo3 {
public static void main(String[] args) {
JPanel north = new JPanel();
north.add(new JLabel("Drag from here:"));
JTextField field = new JTextField(10);
field.setDragEnabled(true);
north.add(field);
final DefaultListModel listModel = new DefaultListModel();
listModel.addElement("first");
listModel.addElement("second");
final JList list = new JList(listModel);
list.setDragEnabled(true);
list.setTransferHandler(new TransferHandler() {
public boolean canImport(TransferHandler.TransferSupport support) {
if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return false;
}
JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
if (dl.getIndex() == -1) {
return false;
} else {
return true;
}
}
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
Transferable transferable = support.getTransferable();
String data;
try {
data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
} catch (Exception e) {
return false;
}
JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
int index = dl.getIndex();
if (dl.isInsert()) {
listModel.add(index, data);
} else {
listModel.set(index, data);
}
Rectangle r = list.getCellBounds(index, index);
list.scrollRectToVisible(r);
return true;
}
});
JScrollPane center = new JScrollPane();
center.setViewportView(list);
list.setDropMode(DropMode.USE_SELECTION);
JPanel cp = new JPanel();
cp.setLayout(new BorderLayout());
cp.add(north, BorderLayout.NORTH);
cp.add(center, BorderLayout.CENTER);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(cp);
frame.pack();
frame.setVisible(true);
}
}
Drag and drop between JTextArea and JTextField
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class DnDBetweenJTextAreaAndJTextFieldDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Drag and Drop Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new JPanel());
JTextField textField = new JTextField(25);
textField.setText("www.jexp.ru");
frame.add(textField);
JTextArea textArea = new JTextArea(4, 25);
textArea.setText("Demonstrating\ndrag and drop");
frame.getContentPane().add(new JScrollPane(textArea));
textArea.setDragEnabled(true);
textField.setDragEnabled(true);
frame.pack();
frame.setVisible(true);
}
}
DropMode.INSERT
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.TransferHandler;
public class DropModeINSERT {
public static void main(String[] args) {
JPanel north = new JPanel();
north.add(new JLabel("Drag from here:"));
JTextField field = new JTextField(10);
field.setDragEnabled(true);
north.add(field);
final DefaultListModel listModel = new DefaultListModel();
listModel.addElement("first");
listModel.addElement("second");
final JList list = new JList(listModel);
list.setDragEnabled(true);
list.setTransferHandler(new TransferHandler() {
public boolean canImport(TransferHandler.TransferSupport support) {
if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return false;
}
JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
if (dl.getIndex() == -1) {
return false;
} else {
return true;
}
}
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
Transferable transferable = support.getTransferable();
String data;
try {
data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
} catch (Exception e) {
return false;
}
JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
int index = dl.getIndex();
if (dl.isInsert()) {
listModel.add(index, data);
} else {
listModel.set(index, data);
}
// Scroll to display the element that was dropped
Rectangle r = list.getCellBounds(index, index);
list.scrollRectToVisible(r);
return true;
}
});
JScrollPane center = new JScrollPane();
center.setViewportView(list);
list.setDropMode(DropMode.INSERT);
JPanel cp = new JPanel();
cp.setLayout(new BorderLayout());
cp.add(north, BorderLayout.NORTH);
cp.add(center, BorderLayout.CENTER);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(cp);
frame.pack();
frame.setVisible(true);
}
}
DropMode.ON
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.TransferHandler;
public class DropModeON {
public static void main(String[] args) {
JPanel north = new JPanel();
north.add(new JLabel("Drag from here:"));
JTextField field = new JTextField(10);
field.setDragEnabled(true);
north.add(field);
final DefaultListModel listModel = new DefaultListModel();
listModel.addElement("first");
listModel.addElement("second");
final JList list = new JList(listModel);
list.setDragEnabled(true);
list.setTransferHandler(new TransferHandler() {
public boolean canImport(TransferHandler.TransferSupport support) {
if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return false;
}
JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
if (dl.getIndex() == -1) {
return false;
} else {
return true;
}
}
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
Transferable transferable = support.getTransferable();
String data;
try {
data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
} catch (Exception e) {
return false;
}
JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
int index = dl.getIndex();
if (dl.isInsert()) {
listModel.add(index, data);
} else {
listModel.set(index, data);
}
// Scroll to display the element that was dropped
Rectangle r = list.getCellBounds(index, index);
list.scrollRectToVisible(r);
return true;
}
});
JScrollPane center = new JScrollPane();
center.setViewportView(list);
list.setDropMode(DropMode.ON);
JPanel cp = new JPanel();
cp.setLayout(new BorderLayout());
cp.add(north, BorderLayout.NORTH);
cp.add(center, BorderLayout.CENTER);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(cp);
frame.pack();
frame.setVisible(true);
}
}
DropMode.ON_OR_INSERT
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.TransferHandler;
public class DropModeON_OR_INSERT {
public static void main(String[] args) {
JPanel north = new JPanel();
north.add(new JLabel("Drag from here:"));
JTextField field = new JTextField(10);
field.setDragEnabled(true);
north.add(field);
final DefaultListModel listModel = new DefaultListModel();
listModel.addElement("first");
listModel.addElement("second");
final JList list = new JList(listModel);
list.setDragEnabled(true);
list.setTransferHandler(new TransferHandler() {
public boolean canImport(TransferHandler.TransferSupport support) {
if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return false;
}
JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
if (dl.getIndex() == -1) {
return false;
} else {
return true;
}
}
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
Transferable transferable = support.getTransferable();
String data;
try {
data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
} catch (Exception e) {
return false;
}
JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
int index = dl.getIndex();
if (dl.isInsert()) {
listModel.add(index, data);
} else {
listModel.set(index, data);
}
// Scroll to display the element that was dropped
Rectangle r = list.getCellBounds(index, index);
list.scrollRectToVisible(r);
return true;
}
});
JScrollPane center = new JScrollPane();
center.setViewportView(list);
list.setDropMode(DropMode.ON_OR_INSERT);
JPanel cp = new JPanel();
cp.setLayout(new BorderLayout());
cp.add(north, BorderLayout.NORTH);
cp.add(center, BorderLayout.CENTER);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(cp);
frame.pack();
frame.setVisible(true);
}
}
Set tree drag mode to DropMode.INSERT
import java.awt.BorderLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.DropMode;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
public class DndTree {
public static void main(String args[]) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel top = new JPanel(new BorderLayout());
JLabel dragLabel = new JLabel("Drag me:");
JTextField text = new JTextField();
text.setDragEnabled(true);
top.add(dragLabel, BorderLayout.WEST);
top.add(text, BorderLayout.CENTER);
f.add(top, BorderLayout.NORTH);
final JTree tree = new JTree();
final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
tree.setTransferHandler(new TransferHandler() {
public boolean canImport(TransferHandler.TransferSupport support) {
if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) {
return false;
}
JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
return dropLocation.getPath() != null;
}
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
TreePath path = dropLocation.getPath();
Transferable transferable = support.getTransferable();
String transferData;
try {
transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor);
} catch (IOException e) {
return false;
} catch (UnsupportedFlavorException e) {
return false;
}
int childIndex = dropLocation.getChildIndex();
if (childIndex == -1) {
childIndex = model.getChildCount(path.getLastPathComponent());
}
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData);
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
model.insertNodeInto(newNode, parentNode, childIndex);
TreePath newPath = path.pathByAddingChild(newNode);
tree.makeVisible(newPath);
tree.scrollRectToVisible(tree.getPathBounds(newPath));
return true;
}
});
JScrollPane pane = new JScrollPane(tree);
f.add(pane, BorderLayout.CENTER);
tree.setDropMode(DropMode.INSERT);
f.setSize(300, 400);
f.setVisible(true);
}
}
Set tree drag mode to DropMode.ON
import java.awt.BorderLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.DropMode;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
public class DndTree {
public static void main(String args[]) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel top = new JPanel(new BorderLayout());
JLabel dragLabel = new JLabel("Drag me:");
JTextField text = new JTextField();
text.setDragEnabled(true);
top.add(dragLabel, BorderLayout.WEST);
top.add(text, BorderLayout.CENTER);
f.add(top, BorderLayout.NORTH);
final JTree tree = new JTree();
final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
tree.setTransferHandler(new TransferHandler() {
public boolean canImport(TransferHandler.TransferSupport support) {
if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) {
return false;
}
JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
return dropLocation.getPath() != null;
}
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
TreePath path = dropLocation.getPath();
Transferable transferable = support.getTransferable();
String transferData;
try {
transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor);
} catch (IOException e) {
return false;
} catch (UnsupportedFlavorException e) {
return false;
}
int childIndex = dropLocation.getChildIndex();
if (childIndex == -1) {
childIndex = model.getChildCount(path.getLastPathComponent());
}
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData);
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
model.insertNodeInto(newNode, parentNode, childIndex);
TreePath newPath = path.pathByAddingChild(newNode);
tree.makeVisible(newPath);
tree.scrollRectToVisible(tree.getPathBounds(newPath));
return true;
}
});
JScrollPane pane = new JScrollPane(tree);
f.add(pane, BorderLayout.CENTER);
tree.setDropMode(DropMode.ON);
f.setSize(300, 400);
f.setVisible(true);
}
}
Set tree drag mode to DropMode.ON_OR_INSERT
import java.awt.BorderLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.DropMode;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
public class DndTree {
public static void main(String args[]) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel top = new JPanel(new BorderLayout());
JLabel dragLabel = new JLabel("Drag me:");
JTextField text = new JTextField();
text.setDragEnabled(true);
top.add(dragLabel, BorderLayout.WEST);
top.add(text, BorderLayout.CENTER);
f.add(top, BorderLayout.NORTH);
final JTree tree = new JTree();
final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
tree.setTransferHandler(new TransferHandler() {
public boolean canImport(TransferHandler.TransferSupport support) {
if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) {
return false;
}
JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
return dropLocation.getPath() != null;
}
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
TreePath path = dropLocation.getPath();
Transferable transferable = support.getTransferable();
String transferData;
try {
transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor);
} catch (IOException e) {
return false;
} catch (UnsupportedFlavorException e) {
return false;
}
int childIndex = dropLocation.getChildIndex();
if (childIndex == -1) {
childIndex = model.getChildCount(path.getLastPathComponent());
}
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData);
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
model.insertNodeInto(newNode, parentNode, childIndex);
TreePath newPath = path.pathByAddingChild(newNode);
tree.makeVisible(newPath);
tree.scrollRectToVisible(tree.getPathBounds(newPath));
return true;
}
});
JScrollPane pane = new JScrollPane(tree);
f.add(pane, BorderLayout.CENTER);
tree.setDropMode(DropMode.ON_OR_INSERT);
f.setSize(300, 400);
f.setVisible(true);
}
}
Set tree DropMode to DropMode.USE_SELECTION
import java.awt.BorderLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.DropMode;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
public class DndTree {
public static void main(String args[]) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel top = new JPanel(new BorderLayout());
JLabel dragLabel = new JLabel("Drag me:");
JTextField text = new JTextField();
text.setDragEnabled(true);
top.add(dragLabel, BorderLayout.WEST);
top.add(text, BorderLayout.CENTER);
f.add(top, BorderLayout.NORTH);
final JTree tree = new JTree();
final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
tree.setTransferHandler(new TransferHandler() {
public boolean canImport(TransferHandler.TransferSupport support) {
if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) {
return false;
}
JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
return dropLocation.getPath() != null;
}
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
TreePath path = dropLocation.getPath();
Transferable transferable = support.getTransferable();
String transferData;
try {
transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor);
} catch (IOException e) {
return false;
} catch (UnsupportedFlavorException e) {
return false;
}
int childIndex = dropLocation.getChildIndex();
if (childIndex == -1) {
childIndex = model.getChildCount(path.getLastPathComponent());
}
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData);
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
model.insertNodeInto(newNode, parentNode, childIndex);
TreePath newPath = path.pathByAddingChild(newNode);
tree.makeVisible(newPath);
tree.scrollRectToVisible(tree.getPathBounds(newPath));
return true;
}
});
JScrollPane pane = new JScrollPane(tree);
f.add(pane, BorderLayout.CENTER);
tree.setDropMode(DropMode.USE_SELECTION);
f.setSize(300, 400);
f.setVisible(true);
}
}
Transfer both Text and Color between JTextField and JTextArea
import java.awt.Color;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.TransferHandler;
import javax.swing.text.JTextComponent;
public class DnDDemo2 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new JPanel());
JTextField textField = new JTextField(25);
textField.setText("Let"s swing higher");
frame.add(textField);
JTextArea textArea = new JTextArea("Demonstrating\ndrag and drop");
textArea.setForeground(Color.red);
frame.add(new JScrollPane(textArea));
textArea.setDragEnabled(true);
textField.setDragEnabled(true);
TextColorTransferHandler transferHandler = new TextColorTransferHandler();
textArea.setTransferHandler(transferHandler);
textField.setTransferHandler(transferHandler);
frame.pack();
frame.setVisible(true);
}
}
class TextColorTransferHandler extends TransferHandler {
public int getSourceActions(JComponent c) {
return COPY_OR_MOVE;
}
protected Transferable createTransferable(JComponent component) {
String text = ((JTextComponent) component).getText();
Color color = component.getForeground();
TextColor transferable = new TextColor(text, color);
return transferable;
}
public boolean canImport(JComponent c, DataFlavor[] flavors) {
return true;
}
public boolean importData(JComponent component, Transferable transferable) {
String colorMimeType = DataFlavor.javaJVMLocalObjectMimeType + ";class=java.awt.Color";
JTextComponent textComponent = (JTextComponent) component;
try {
DataFlavor colorFlavor = new DataFlavor(colorMimeType);
Color color = (Color) transferable.getTransferData(colorFlavor);
String text = (String) transferable.getTransferData(DataFlavor.stringFlavor);
textComponent.setForeground(color);
textComponent.setText(text);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
class TextColor implements Transferable {
private String text;
private Color color;
private DataFlavor[] flavors;
public TextColor(String text, Color color) {
String colorMimeType = DataFlavor.javaJVMLocalObjectMimeType + ";class=java.awt.Color";
DataFlavor colorFlavor = null;
try {
colorFlavor = new DataFlavor(colorMimeType);
} catch (ClassNotFoundException e) {
}
flavors = new DataFlavor[2];
flavors[0] = DataFlavor.stringFlavor;
flavors[1] = colorFlavor;
this.text = text;
this.color = color;
}
public DataFlavor[] getTransferDataFlavors() {
return (DataFlavor[]) flavors.clone();
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
for (int i = 0; i < flavors.length; i++) {
if (flavor.equals(flavors[i])) {
return true;
}
}
return false;
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (flavor.equals(flavors[0])) {
return text;
} else if (flavor.equals(flavors[1])) {
return color;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
//Reference:
//Java 6 New Features: A Tutorial
//by Budi Kurniawan
//Brainy Software Corp. 2006
//Chapter 4 - Networking
//# ISBN-10: 0975212885
//# ISBN-13: 978-0975212882