Java by API/javax.swing/TransferHandler — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:19, 31 мая 2010
extends TransferHandler
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.TransferHandler;
public class MainClass extends JFrame {
public MainClass() {
super("Drop Target");
JComponent cp = (JComponent) getContentPane();
cp.setTransferHandler(new MyFileTransferHandler()); // see below
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(150, 150);
}
public static void main(String[] args) {
new MainClass().setVisible(true);
}
}
class MyFileTransferHandler extends TransferHandler {
public boolean canImport(JComponent com, DataFlavor[] dataFlavors) {
for (int i = 0; i < dataFlavors.length; i++) {
DataFlavor flavor = dataFlavors[i];
if (flavor.equals(DataFlavor.javaFileListFlavor)) {
System.out.println("canImport: JavaFileList FLAVOR: " + flavor);
return true;
}
if (flavor.equals(DataFlavor.stringFlavor)) {
System.out.println("canImport: String FLAVOR: " + flavor);
return true;
}
System.err.println("canImport: Rejected Flavor: " + flavor);
}
return false;
}
public boolean importData(JComponent comp, Transferable t) {
DataFlavor[] flavors = t.getTransferDataFlavors();
System.out.println("Trying to import:" + t);
for (int i = 0; i < flavors.length; i++) {
DataFlavor flavor = flavors[i];
try {
if (flavor.equals(DataFlavor.javaFileListFlavor)) {
System.out.println("importData: FileListFlavor");
List l = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
Iterator iter = l.iterator();
while (iter.hasNext()) {
File file = (File) iter.next();
System.out.println("GOT FILE: " + file.getCanonicalPath());
}
return true;
} else if (flavor.equals(DataFlavor.stringFlavor)) {
System.out.println("importData: String Flavor");
String fileOrURL = (String) t.getTransferData(flavor);
System.out.println("GOT STRING: " + fileOrURL);
try {
URL url = new URL(fileOrURL);
System.out.println("Valid URL: " + url.toString());
return true;
} catch (MalformedURLException ex) {
System.err.println("Not a valid URL");
return false;
}
} else {
System.out.println("importData rejected: " + flavor);
}
} catch (IOException ex) {
System.err.println("IOError getting data: " + ex);
} catch (UnsupportedFlavorException e) {
System.err.println("Unsupported Flavor: " + e);
}
}
return false;
}
}
new TransferHandler(String property)
/**
* Demonstrate various aspects of Swing "data transfer".
* @author Ian Darwin, http://www.darwinsys.ru
* @author Jonathan Fuerth, http://www.SQLPower.ca
*/
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.TransferHandler;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass extends JFrame {
public static void main(String[] args) {
new MainClass().setVisible(true);
}
private JTextField tf;
private JLabel l;
private JComboBox propertyComboBox;
public MainClass() {
Container cp = new Box(BoxLayout.X_AXIS);
setContentPane(cp);
JPanel firstPanel = new JPanel();
propertyComboBox = new JComboBox();
propertyComboBox.addItem("text");
propertyComboBox.addItem("font");
propertyComboBox.addItem("background");
propertyComboBox.addItem("foreground");
firstPanel.add(propertyComboBox);
cp.add(firstPanel);
cp.add(Box.createGlue());
tf = new JTextField("Hello");
tf.setForeground(Color.RED);
tf.setDragEnabled(true);
cp.add(tf);
cp.add(Box.createGlue());
l = new JLabel("Hello");
l.setBackground(Color.YELLOW);
cp.add(l);
cp.add(Box.createGlue());
JSlider stryder = new JSlider(SwingConstants.VERTICAL);
stryder.setMinimum(10);
stryder.setValue(14);
stryder.setMaximum(72);
stryder.setMajorTickSpacing(10);
stryder.setPaintTicks(true);
cp.add(stryder);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setMyTransferHandlers((String) propertyComboBox.getSelectedItem());
MouseListener myDragListener = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
JComponent c = (JComponent) e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
}
};
l.addMouseListener(myDragListener);
propertyComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ce) {
JComboBox bx = (JComboBox) ce.getSource();
String prop = (String) bx.getSelectedItem();
setMyTransferHandlers(prop);
}
});
tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JTextField jtf = (JTextField) evt.getSource();
String fontName = jtf.getText();
Font font = new Font(fontName, Font.BOLD, 18);
tf.setFont(font);
}
});
stryder.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent evt) {
JSlider sl = (JSlider) evt.getSource();
Font oldf = tf.getFont();
Font newf = oldf.deriveFont((float) sl.getValue());
tf.setFont(newf);
}
});
}
private void setMyTransferHandlers(String s) {
TransferHandler th = new TransferHandler(s);
tf.setTransferHandler(th);
l.setTransferHandler(th);
}
}
TransferHandler.COPY
/**
* Demonstrate various aspects of Swing "data transfer".
* @author Ian Darwin, http://www.darwinsys.ru
* @author Jonathan Fuerth, http://www.SQLPower.ca
*/
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.TransferHandler;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass extends JFrame {
public static void main(String[] args) {
new MainClass().setVisible(true);
}
private JTextField tf;
private JLabel l;
private JComboBox propertyComboBox;
public MainClass() {
Container cp = new Box(BoxLayout.X_AXIS);
setContentPane(cp);
JPanel firstPanel = new JPanel();
propertyComboBox = new JComboBox();
propertyComboBox.addItem("text");
propertyComboBox.addItem("font");
propertyComboBox.addItem("background");
propertyComboBox.addItem("foreground");
firstPanel.add(propertyComboBox);
cp.add(firstPanel);
cp.add(Box.createGlue());
tf = new JTextField("Hello");
tf.setForeground(Color.RED);
tf.setDragEnabled(true);
cp.add(tf);
cp.add(Box.createGlue());
l = new JLabel("Hello");
l.setBackground(Color.YELLOW);
cp.add(l);
cp.add(Box.createGlue());
JSlider stryder = new JSlider(SwingConstants.VERTICAL);
stryder.setMinimum(10);
stryder.setValue(14);
stryder.setMaximum(72);
stryder.setMajorTickSpacing(10);
stryder.setPaintTicks(true);
cp.add(stryder);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setMyTransferHandlers((String) propertyComboBox.getSelectedItem());
MouseListener myDragListener = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
JComponent c = (JComponent) e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
}
};
l.addMouseListener(myDragListener);
propertyComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ce) {
JComboBox bx = (JComboBox) ce.getSource();
String prop = (String) bx.getSelectedItem();
setMyTransferHandlers(prop);
}
});
tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JTextField jtf = (JTextField) evt.getSource();
String fontName = jtf.getText();
Font font = new Font(fontName, Font.BOLD, 18);
tf.setFont(font);
}
});
stryder.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent evt) {
JSlider sl = (JSlider) evt.getSource();
Font oldf = tf.getFont();
Font newf = oldf.deriveFont((float) sl.getValue());
tf.setFont(newf);
}
});
}
private void setMyTransferHandlers(String s) {
TransferHandler th = new TransferHandler(s);
tf.setTransferHandler(th);
l.setTransferHandler(th);
}
}