树拖动模式DropMode.ON_OR_INSERT : 拖曳 « Swing « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » Swing » 拖曳 
14. 112. 16. 树拖动模式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 = (DefaultTreeModeltree.getModel();
    tree.setTransferHandler(new TransferHandler() {
      public boolean canImport(TransferHandler.TransferSupport support) {
        if (!support.isDataFlavorSupported(DataFlavor.stringFlavor|| !support.isDrop()) {
          return false;
        }
        JTree.DropLocation dropLocation = (JTree.DropLocationsupport.getDropLocation();
        return dropLocation.getPath() != null;
      }

      public boolean importData(TransferHandler.TransferSupport support) {
        if (!canImport(support)) {
          return false;
        }
        JTree.DropLocation dropLocation = (JTree.DropLocationsupport.getDropLocation();
        TreePath path = dropLocation.getPath();
        Transferable transferable = support.getTransferable();

        String transferData;
        try {
          transferData = (Stringtransferable.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 = (DefaultMutableTreeNodepath.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(300400);
    f.setVisible(true);
  }
}
14. 112. 拖曳
14. 112. 1. 基本拖放
14. 112. 2. 从JLabel拖动文字从JLabel拖动文字
14. 112. 3. 拖放图片拖放图片
14. 112. 4. 拖放图标
14. 112. 5. 实施DragGestureListener
14. 112. 6. Dragging and dropping text between a text area, a list, and a tableDragging and dropping text between a text area, a list, and a table
14. 112. 7. 拖放JTextArea和JTextField
14. 112. 8. Transfer both Text and Color between JTextField and JTextArea
14. 112. 9. 拖放JList和JTextField
14. 112. 10. DropMode.ON
14. 112. 11. DropMode.INSERT
14. 112. 12. DropMode.ON_OR_INSERT
14. 112. 13. 树DropMode以DropMode.USE_SELECTION
14. 112. 14. 树拖动模式DropMode.ON
14. 112. 15. 树拖动模式DropMode.INSERT
14. 112. 16. 树拖动模式DropMode.ON_OR_INSERT
14. 112. 17. 选择下拉事件
14. 112. 18. 各种下拉事件
14. 112. 19. 表格拖放
14. 112. 20. Create a drag source a drop target and a transferable object.
14. 112. 21. 组件拖动
14. 112. 22. 拖动发起的动作
14. 112. 23. Illustrates cut, copy, paste and drag and drop using three instances of JList
14. 112. 24. 位置敏感拖放
14. 112. 25. Demonstration of the top-level TransferHandler support on JFrame
14. 112. 26. Drag-and-Drop customization: drag the foreground color from the first label and drop it as the background color into the second one
14. 112. 27. Demonstrates how to add copy and drag support to a Swing component with TransferHandler
14. 112. 28. ScribblePane allows individual PolyLine lines to be selected, cut, copied, pasted, dragged, and dropped
14. 112. 29. Built-in drag and drop support: utilize a TransferHandler class
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.