Swing之拖动篇:将文本中的内容拖动到表中

拖动操作的很多地方都有用到,比如我们的用QQ的时候也可以拖动文本信息。下面这个例子是通过重写TransferHandler类来实现控件之间的数据拖动。

TransferHandler类用于处理往返于 Swing 组件的 Transferable 的传输。该 Transferable 用于表示通过往/返于剪贴板的剪切、复制或粘贴操作进行交换的数据。它还在拖放操作中使用,以表示对组件进行拖动和放置组件。Swing 提供自动支持剪切、复制和粘贴的键盘绑定的功能,该键盘绑定使用由此类的实现提供的功能。Swing 还提供自动支持拖放操作的功能,该拖放操作使用由此类的实现提供的功能。Swing 开发人员可以主要通过在 Swing 组件上设置 transferHandler 属性专门指定传输语义。

import java.awt.BorderLayout;   
import java.awt.Dimension;   
import java.awt.Rectangle;   
import java.awt.datatransfer.DataFlavor;   
import java.awt.datatransfer.UnsupportedFlavorException;   
import java.awt.event.ActionEvent;   
import java.awt.event.ActionListener;   
import java.awt.event.MouseAdapter;   
import java.awt.event.MouseEvent;   
import java.io.IOException;   
  
import javax.swing.BorderFactory;   
import javax.swing.Box;   
import javax.swing.BoxLayout;   
import javax.swing.DefaultListModel;   
import javax.swing.DropMode;   
import javax.swing.JCheckBoxMenuItem;   
import javax.swing.JFrame;   
import javax.swing.JLabel;   
import javax.swing.JList;   
import javax.swing.JMenu;   
import javax.swing.JMenuBar;   
import javax.swing.JMenuItem;   
import javax.swing.JPanel;   
import javax.swing.JScrollPane;   
import javax.swing.JTable;   
import javax.swing.ListSelectionModel;   
import javax.swing.SwingUtilities;   
import javax.swing.TransferHandler;   
import javax.swing.UIManager;   
import javax.swing.table.DefaultTableModel;   
  
public class FillViewportHeightDemo extends JFrame implements ActionListener {   
  
  private DefaultListModel model = new DefaultListModel();   
  private int count = 0;   
  private JTable table;   
  private JCheckBoxMenuItem fillBox;   
  private DefaultTableModel tableModel;   
  
  private static String getNextString(int count) {   
    StringBuffer buf = new StringBuffer();   
    for (int i = 0; i < 5; i++) {   
      buf.append(String.valueOf(count));   
      buf.append(",");   
    }   
  
    // remove last newline   
    buf.deleteCharAt(buf.length() - 1);   
    return buf.toString();   
  }   
  
  private static DefaultTableModel getDefaultTableModel() {   
    String[] cols = { "Foo", "Toto", "Kala", "Pippo", "Boing" };   
    return new DefaultTableModel(null, cols);   
  }   
  
  public FillViewportHeightDemo() {   
    super("Empty Table DnD Demo");   
  
    tableModel = getDefaultTableModel();   
    table = new JTable(tableModel);   
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);   
    table.setDropMode(DropMode.INSERT_ROWS);   
  
    //增加表格拖动事件   
    table.setTransferHandler(new TransferHandler() {   
  
      //指示一个组件在实际尝试导入给定数据 flavor 的集合之前是否接受导入。   
      public boolean canImport(TransferSupport support) {   
        // for the demo, we'll only support drops (not clipboard paste)   
        if (!support.isDrop()) {   
          return false;   
        }   
  
        // we only import Strings   
        if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {   
          return false;   
        }   
  
        return true;   
      }   
  
      //导致从剪贴板或 DND 放置操作向组件的传输。   
      public boolean importData(TransferSupport support) {   
        // if we can't handle the import, say so   
        if (!canImport(support)) {   
          return false;   
        }   
  
        // fetch the drop location   
        JTable.DropLocation dl = (JTable.DropLocation) support   
            .getDropLocation();   
  
        int row = dl.getRow();   
  
        // fetch the data and bail if this fails   
        String data;   
        try {   
          //获得拖动数据   
          data = (String) support.getTransferable().getTransferData(   
              DataFlavor.stringFlavor);   
        } catch (UnsupportedFlavorException e) {   
          return false;   
        } catch (IOException e) {   
          return false;   
        }   
  
        String[] rowData = data.split(",");   
           
        //将数据增加到表格中   
        tableModel.insertRow(row, rowData);   
  
        Rectangle rect = table.getCellRect(row, 0, false);   
        if (rect != null) {   
          table.scrollRectToVisible(rect);   
        }   
  
        // demo stuff - remove for blog   
        model.removeAllElements();   
        model.insertElementAt(getNextString(count++), 0);   
        // end demo stuff   
  
        return true;   
      }   
    });   
  
    JList dragFrom = new JList(model);   
    dragFrom.setFocusable(false);   
    dragFrom.setPrototypeCellValue(getNextString(100));   
    model.insertElementAt(getNextString(count++), 0);   
    dragFrom.setDragEnabled(true);   
    dragFrom.setBorder(BorderFactory.createLoweredBevelBorder());   
  
    dragFrom.addMouseListener(new MouseAdapter() {   
      public void mouseClicked(MouseEvent me) {   
        if (SwingUtilities.isLeftMouseButton(me) && me.getClickCount() % 2 == 0) {   
          String text = (String) model.getElementAt(0);   
          String[] rowData = text.split(",");   
          tableModel.insertRow(table.getRowCount(), rowData);   
          model.removeAllElements();   
          model.insertElementAt(getNextString(count++), 0);   
        }   
      }   
    });   
  
    JPanel p = new JPanel();   
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));   
    JPanel wrap = new JPanel();   
    wrap.add(new JLabel("Drag from here:"));   
    wrap.add(dragFrom);   
    p.add(Box.createHorizontalStrut(4));   
    p.add(Box.createGlue());   
    p.add(wrap);   
    p.add(Box.createGlue());   
    p.add(Box.createHorizontalStrut(4));   
    getContentPane().add(p, BorderLayout.NORTH);   
  
    JScrollPane sp = new JScrollPane(table);   
    getContentPane().add(sp, BorderLayout.CENTER);   
    fillBox = new JCheckBoxMenuItem("Fill Viewport Height");   
    fillBox.addActionListener(this);   
  
    JMenuBar mb = new JMenuBar();   
    JMenu options = new JMenu("Options");   
    mb.add(options);   
    setJMenuBar(mb);   
  
    JMenuItem clear = new JMenuItem("Reset");   
    clear.addActionListener(this);   
    options.add(clear);   
    options.add(fillBox);   
  
    getContentPane().setPreferredSize(new Dimension(260, 180));   
  }   
  
  public void actionPerformed(ActionEvent ae) {   
    if (ae.getSource() == fillBox) {   
      table.setFillsViewportHeight(fillBox.isSelected());   
    } else {   
      tableModel.setRowCount(0);   
      count = 0;   
      model.removeAllElements();   
      model.insertElementAt(getNextString(count++), 0);   
    }   
  }   
  
  private static void createAndShowGUI() {   
    // Create and set up the window.   
    FillViewportHeightDemo test = new FillViewportHeightDemo();   
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  
    // Display the window.   
    test.pack();   
    test.setVisible(true);   
  }   
  
  public static void main(String[] args) {   
    SwingUtilities.invokeLater(new Runnable() {   
      public void run() {   
        // Turn off metal's use of bold fonts   
        UIManager.put("swing.boldMetal", Boolean.FALSE);   
        createAndShowGUI();   
      }   
    });   
  }   
}  


 

实现文件的拖拽功能 import java.awt.AlphaComposite; import java.awt.Component; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.dnd.DnDConstants; import java.awt.dnd.DragGestureEvent; import java.awt.dnd.DragGestureListener; import java.awt.dnd.DragSource; import java.awt.dnd.DragSourceDragEvent; import java.awt.dnd.DragSourceDropEvent; import java.awt.dnd.DragSourceEvent; import java.awt.dnd.DragSourceListener; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.swing.Icon; import javax.swing.JLabel; import javax.swing.JTree; import javax.swing.Timer; import javax.swing.event.TreeExpansionEvent; import javax.swing.event.TreeExpansionListener; import javax.swing.filechooser.FileSystemView; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; class DragTree extends JTree implements DragGestureListener, DragSourceListener, DropTargetListener { /** * */ private static final long serialVersionUID = 1L; BufferedImage ghostImage; private Rectangle2D ghostRect = new Rectangle2D.Float(); private Point ptOffset = new Point(); private Point lastPoint = new Point(); private TreePath lastPath; private Timer hoverTimer; FileNode sourceNode; public DragTree() { DragSource dragSource = DragSource.getDefaultDragSource(); dragSource.createDefaultDragGestureRecognizer(this, // component where // drag originates DnDConstants.ACTION_COPY_OR_MOVE, // actions this); // drag gesture recognizer setModel(createTreeModel()); addTreeExpansionListener(new TreeExpansionListener() { public void treeCollapsed(TreeExpansionEvent e) { } public void treeExpanded(TreeExpansionEvent e) { TreePath path = e.getPath(); if (path != null) { FileNode node = (FileNode) path.getLastPathComponent(); if (!node.isExplored()) { DefaultTreeModel model = (DefaultTreeModel) getModel(); node.explore(); model.nodeStructureChanged(node); } } } }); this.setCellRenderer(new DefaultTreeCellRenderer() { public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { TreePath tp = tree.getPathForRow(row); if (tp != null) { FileNode node = (FileNode) tp.getLastPathComponent(); File f = node.getFile(); try { Icon icon = FileSystemView.getFileSystemView() .getSystemIcon(f); this.setIcon(icon); this.setLeafIcon(icon); this.setOpenIcon(icon); this.setClosedIcon(icon); this.setDisabledIcon(icon); } catch (Exception e) { e.printStackTrace(); } } return super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); } }); super.setScrollsOnExpand(true); new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this); // Set up a hover timer, so that a node will be automatically expanded // or collapsed // if the user lingers on it for more than a short time hoverTimer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { if (lastPath == null) { return; } if (getRowForPath(lastPath) == 0) return; // Do nothing if we are hovering over the root node if (isExpanded(lastPath)) collapsePath(lastPath); else expandPath(lastPath); } }); hoverTimer.setRepeats(false); // Set timer to one-shot mode this.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); int modifiers = e.getModifiers(); if (code == 'v' || code == 'V') { System.out.println("find v"); System.out.println("modifiers:" + modifiers + "\t" + ((modifiers & KeyEvent.CTRL_MASK) != 0)); } if ((modifiers & KeyEvent.CTRL_MASK) != 0 && (code == 'v' || code == 'V')) { Transferable tr = Toolkit.getDefaultToolkit() .getSystemClipboard().getContents(null); TreePath path = getSelectionPath(); if (path == null) { return; } FileNode node = (FileNode) path.getLastPathComponent(); if (node.isDirectory()) { System.out.println("file cp"); try { List list = (List) (tr .getTransferData(DataFlavor.javaFileListFlavor)); Iterator iterator = list.iterator(); File parent = node.getFile(); while (iterator.hasNext()) { File f = (File) iterator.next(); cp(f, new File(parent, f.getName())); } node.reexplore(); } catch (Exception ioe) { ioe.printStackTrace(); } updateUI(); } } } }); } public void dragGestureRecognized(DragGestureEvent e) { // drag anything ... TreePath path = getLeadSelectionPath(); if (path == null) return; FileNode node = (FileNode) path.getLastPathComponent(); sourceNode = node; // Work out the offset of the drag point from the TreePath bounding // rectangle origin Rectangle raPath = getPathBounds(path); Point ptDragOrigin = e.getDragOrigin(); ptOffset.setLocation(ptDragOrigin.x - raPath.x, ptDragOrigin.y - raPath.y); // Get the cell renderer (which is a JLabel) for the path being dragged int row = this.getRowForLocation(ptDragOrigin.x, ptDragOrigin.y); JLabel lbl = (JLabel) getCellRenderer().getTreeCellRendererComponent( this, // tree path.getLastPathComponent(), // value false, // isSelected (dont want a colored background) isExpanded(path), // isExpanded getModel().isLeaf(path.getLastPathComponent()), // isLeaf row, // row (not important for rendering) false // hasFocus (dont want a focus rectangle) ); lbl.setSize((int) raPath.getWidth(), (int) raPath.getHeight()); // <-- // The // layout // manager // would // normally // do // this // Get a buffered image of the selection for dragging a ghost image this.ghostImage = new BufferedImage((int) raPath.getWidth(), (int) raPath.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2 = ghostImage.createGraphics(); // Ask the cell renderer to paint itself into the BufferedImage g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f)); // Make the image ghostlike lbl.paint(g2); g2.dispose(); // this.getGraphics().drawImage(ghostImage, e.getDragOrigin().x, // e.getDragOrigin().y, this); e.startDrag( null, // cursor ghostImage, new Point(5, 5), new StringSelection(getFilename()), // transferable this); // drag source listener } public void dragDropEnd(DragSourceDropEvent e) { ghostImage = null; sourceNode = null; } public void dragEnter(DragSourceDragEvent e) { } public void dragExit(DragSourceEvent e) { if (!DragSource.isDragImageSupported()) { repaint(ghostRect.getBounds()); } } public void dragOver(DragSourceDragEvent e) { } public void dropActionChanged(DragSourceDragEvent e) { } public String getFilename() { TreePath path = getLeadSelectionPath(); FileNode node = (FileNode) path.getLastPathComponent(); return ((File) node.getUserObject()).getAbsolutePath(); } private DefaultTreeModel createTreeModel() { File root = FileSystemView.getFileSystemView().getRoots()[0]; FileNode rootNode = new FileNode(root); rootNode.explore(); return new DefaultTreeModel(rootNode); } public void dragEnter(DropTargetDragEvent dtde) { } public void dragOver(DropTargetDragEvent dtde) { Point pt = dtde.getLocation(); if (pt.equals(lastPoint)) { return; } if (ghostImage != null) { Graphics2D g2 = (Graphics2D) getGraphics(); // If a drag image is not supported by the platform, then draw my // own drag image if (!DragSource.isDragImageSupported()) { paintImmediately(ghostRect.getBounds()); // Rub out the last // ghost image and cue // line // And remember where we are about to draw the new ghost image ghostRect.setRect(pt.x - ptOffset.x, pt.y - ptOffset.y, ghostImage.getWidth(), ghostImage.getHeight()); g2.drawImage((ghostImage), AffineTransform .getTranslateInstance(ghostRect.getX(), ghostRect.getY()), null); } } TreePath path = getClosestPathForLocation(pt.x, pt.y); if (!(path == lastPath)) { lastPath = path; hoverTimer.restart(); } } public void dropActionChanged(DropTargetDragEvent dtde) { } public void drop(DropTargetDropEvent e) { try { DataFlavor stringFlavor = DataFlavor.stringFlavor; Transferable tr = e.getTransferable(); TreePath path = this.getPathForLocation(e.getLocation().x, e.getLocation().y); if (path == null) { e.rejectDrop(); return; } FileNode node = (FileNode) path.getLastPathComponent(); if (e.isDataFlavorSupported(DataFlavor.javaFileListFlavor) && node.isDirectory()) { e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); System.out.println("file cp"); List list = (List) (e.getTransferable() .getTransferData(DataFlavor.javaFileListFlavor)); Iterator iterator = list.iterator(); File parent = node.getFile(); while (iterator.hasNext()) { File f = (File) iterator.next(); cp(f, new File(parent, f.getName())); } node.reexplore(); e.dropComplete(true); this.updateUI(); } else if (e.isDataFlavorSupported(stringFlavor) && node.isDirectory()) { String filename = (String) tr.getTransferData(stringFlavor); if (filename.endsWith(".txt") || filename.endsWith(".java") || filename.endsWith(".jsp") || filename.endsWith(".html") || filename.endsWith(".htm")) { e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); File f = new File(filename); if (f.exists()) { f.renameTo(new File(node.getFile(), f.getName())); node.reexplore(); ((FileNode) sourceNode.getParent()).remove(sourceNode); e.dropComplete(true); this.updateUI(); } else { e.rejectDrop(); } } else { e.rejectDrop(); } } else { e.rejectDrop(); } } catch (IOException ioe) { ioe.printStackTrace(); } catch (UnsupportedFlavorException ufe) { ufe.printStackTrace(); } finally { ghostImage = null; this.repaint(); } } private void cp(File src, File dest) throws IOException { if (src.isDirectory()) { if (!dest.exists()) { boolean ret = dest.mkdir(); if (ret == false) return; } File[] fs = src.listFiles(); for (int i = 0; i < fs.length; i++) { cp(fs[i], new File(dest, fs[i].getName())); } return; } byte[] buf = new byte[1024]; FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(dest); int len; try { while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } finally { in.close(); out.close(); } } public void dragExit(DropTargetEvent dte) { } } class FileNode extends DefaultMutableTreeNode { private boolean explored = false; public FileNode(File file) { setUserObject(file); } public boolean getAllowsChildren() { return isDirectory(); } public boolean isLeaf() { return !isDirectory(); } public File getFile() { return (File) getUserObject(); } public boolean isExplored() { return explored; } public boolean isDirectory() { File file = getFile(); return file.isDirectory(); } public String toString() { File file = (File) getUserObject(); String filename = file.toString(); int index = filename.lastIndexOf(File.separator); return (index != -1 && index != filename.length() - 1) ? filename .substring(index + 1) : filename; } public void explore() { if (!isDirectory()) return; if (!isExplored()) { File file = getFile(); File[] children = file.listFiles(); for (int i = 0; i < children.length; ++i) { if (children[i].isDirectory()) add(new FileNode(children[i])); } for (int i = 0; i < children.length; ++i) { if (!children[i].isDirectory()) add(new FileNode(children[i])); } explored = true; } } public void reexplore() { this.removeAllChildren(); explored = false; explore(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值