代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
树形框动态拖放效果
作者:
DDT
/ 发布于
2012/6/12
/
514
树形框动态拖放效果
<div>public class DnDJTree extends JTree implements DragSourceListener, DropTargetListener, DragGestureListener { static DataFlavor localObjectFlavor; static { try {</div> <div> localObjectFlavor = new DataFlavor (DataFlavor.javaJVMLocalObjectMimeType); } catch (ClassNotFoundException cnfe) { cnfe.printStackTrace(); } } static DataFlavor[] supportedFlavors = { localObjectFlavor }; DragSource dragSource; DropTarget dropTarget; TreeNode dropTargetNode = null; TreeNode draggedNode = null; public DnDJTree () { super(); setCellRenderer (new DnDTreeCellRenderer()); setModel (new DefaultTreeModel(new DefaultMutableTreeNode("default"))); dragSource = new DragSource(); DragGestureRecognizer dgr = dragSource.createDefaultDragGestureRecognizer (this, DnDConstants.ACTION_MOVE, this); dropTarget = new DropTarget (this, this); } <div>// DragGestureListener public void dragGestureRecognized (DragGestureEvent dge) { System.out.println ("dragGestureRecognized"); // find object at this x,y Point clickPoint = dge.getDragOrigin(); TreePath path = getPathForLocation (clickPoint.x, clickPoint.y); if (path == null) {</div> <div>System.out.println ("not on a node"); return; } draggedNode = (TreeNode) path.getLastPathComponent(); Transferable trans = new RJLTransferable (draggedNode); dragSource.startDrag (dge,Cursor.getDefaultCursor(), trans, this); } // DragSourceListener events public void dragDropEnd (DragSourceDropEvent dsde) {</div> <div>System.out.println ("dragDropEnd()"); dropTargetNode = null; draggedNode = null; repaint();</div> <div>} public void dragEnter (DragSourceDragEvent dsde) {} public void dragExit (DragSourceEvent dse) {} public void dragOver (DragSourceDragEvent dsde) {} public void dropActionChanged (DragSourceDragEvent dsde) {} // DropTargetListener events public void dragEnter (DropTargetDragEvent dtde) {</div> <div>System.out.println ("dragEnter"); dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);</div> <div>System.out.println ("accepted dragEnter"); } public void dragExit (DropTargetEvent dte) {} public void dragOver (DropTargetDragEvent dtde) {</div> <div>// figure out which cell it's over, no drag to self Point dragPoint = dtde.getLocation(); TreePath path = getPathForLocation (dragPoint.x, dragPoint.y); if (path == null)</div> <div>dropTargetNode = null; else dropTargetNode = (TreeNode) path.getLastPathComponent();</div> <div>repaint(); } public void drop (DropTargetDropEvent dtde) { System.out.println ("drop()!"); Point dropPoint = dtde.getLocation(); // int index = locationToIndex (dropPoint); TreePath path = getPathForLocation (dropPoint.x, dropPoint.y); System.out.println ("drop path is " + path); boolean dropped = false; try { dtde.acceptDrop (DnDConstants.ACTION_MOVE); System.out.println ("accepted"); Object droppedObject =</div> <div>dtde.getTransferable().getTransferData(localObjectFlavor); MutableTreeNode droppedNode = null; if (droppedObject instanceof MutableTreeNode) { // remove from old location droppedNode = (MutableTreeNode) droppedObject; ((DefaultTreeModel)getModel()). removeNodeFromParent(droppedNode); } else { droppedNode = new DefaultMutableTreeNode (droppedObject); } // insert into spec'd path. if dropped into a parent // make it last child of that parent DefaultMutableTreeNode dropNode = (DefaultMutableTreeNode) path.getLastPathComponent(); if (dropNode.isLeaf()) { DefaultMutableTreeNode parent = (DefaultMutableTreeNode) dropNode.getParent(); int index = parent.getIndex (dropNode); ((DefaultTreeModel)getModel()).insertNodeInto (droppedNode, parent, index); } else { ((DefaultTreeModel)getModel()).insertNodeInto (droppedNode, dropNode, dropNode.getChildCount()); } dropped = true; } catch (Exception e) {</div> <div> e.printStackTrace(); } dtde.dropComplete (dropped); } public void dropActionChanged (DropTargetDragEvent dtde) {} public static void main (String[] args) { JTree tree = new DnDJTree(); DefaultMutableTreeNode root = new DefaultMutableTreeNode("People DefaultMutableTreeNode set1 = new DefaultMutableTreeNode("Set 1"); DefaultMutableTreeNode set2 = new DefaultMutableTreeNode("Set 2"); DefaultMutableTreeNode set3 = new DefaultMutableTreeNode("Set 3"); set1.add (new DefaultMutableTreeNode ("Chris")); set1.add (new DefaultMutableTreeNode ("Kelly")); set1.add (new DefaultMutableTreeNode ("Keagan")); set2.add (new DefaultMutableTreeNode ("Joshua")); set2.add (new DefaultMutableTreeNode ("Kimi")); set3.add (new DefaultMutableTreeNode ("Michael")); set3.add (new DefaultMutableTreeNode ("Don")); set3.add (new DefaultMutableTreeNode ("Daniel")); root.add (set1); root.add (set2); set2.add (set3); DefaultTreeModel mod = new DefaultTreeModel (root); tree.setModel (mod); // expand all for (int i=0; i<tree.getRowCount(); i++) tree.expandRow (i); // show tree JScrollPane scroller = new JScrollPane (tree, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); JFrame frame = new JFrame ("DnD JTree"); frame.getContentPane().add (scroller); frame.pack(); frame.setVisible(true); } class RJLTransferable implements Transferable { Object object; public RJLTransferable (Object o) { object = o; } public Object getTransferData(DataFlavor df) throws UnsupportedFlavorException, IOException { if (isDataFlavorSupported (df)) return object; else throw new UnsupportedFlavorException(df); } public boolean isDataFlavorSupported (DataFlavor df) { return (df.equals (localObjectFlavor)); } public DataFlavor[] getTransferDataFlavors () { return supportedFlavors; } } <div></div> <div>The other helper inner class is where a lot of the "good stuff" happens. DnDJTreeCellRenderer, listed in the following code, has to be aware of what node is the potential drop targetthis is an instance variable set by dragOver( ) in the main classand if the node to be rendered is the drop target, it draws it differently:</div> <div>class DnDTreeCellRenderer extends DefaultTreeCellRenderer { boolean isTargetNode; boolean isTargetNodeLeaf; boolean isLastItem; intBOTTOM_PAD = 30; public DnDTreeCellRenderer() { super(); } public Component getTreeCellRendererComponent (JTree tree, Object value, boolean isSelected, boolean isExpanded, boolean isLeaf, int row, boolean hasFocus) { isTargetNode = (value == dropTargetNode); isTargetNodeLeaf = (isTargetNode &&</div> <div> ((TreeNode)value).isLeaf()); // isLastItem = (index == list.getModel().getSize()-1); boolean showSelected = isSelected & (dropTargetNode == null); return super.getTreeCellRendererComponent (tree, value, isSelected, isExpanded, isLeaf, row, hasFocus); } public void paintComponent (Graphics g) { super.paintComponent(g); if (isTargetNode) { g.setColor(Color.black); if (isTargetNodeLeaf) { g.drawLine (0, 0, getSize().width, 0); } else { g.drawRect (0, 0, getSize().width-1, getSize().height-1); } } } } } </div>
试试其它关键字
树形框动态拖放效果
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
DDT
贡献的其它代码
(
160
)
.
Oracle统计表的数据行和数据块信息
.
html标签闭合检测与修复
.
Powershell日期计算
.
Powershell的Base64编解码
.
Powershell并行循环
.
Powershell目录中搜索文本
.
Powershell枚举远程机器上的本地权限组
.
VBScript解析csv文件
.
快速排序之Powershell
.
批处理输出格式化时间字符串
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3