分享
 
 
 

两个JTree之间的单向拖拽

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

最近因为在一个项目中要实现JTree之间的拖拽,刚刚学习开始学习Java,参照网上的代码完成的,现在贴出来共享,希望对和我一样的菜鸟有点帮助,有什么错误的话还请多多指正。

DragSourceTree.java

package aabbcc;

import javax.swing.JTree;

import java.awt.dnd.DragGestureListener;

import java.awt.dnd.DragSourceListener;

import java.awt.dnd.DragSourceDragEvent;

import java.awt.dnd.DragSourceEvent;

import java.awt.dnd.DragSourceDropEvent;

import java.awt.dnd.DragGestureEvent;

import java.awt.dnd.DragSource;

import javax.swing.tree.DefaultMutableTreeNode;

import java.awt.datatransfer.StringSelection;

/**

* <p>Title: JTree之间的拖拽中的DragSourceTree</p>

* <p>Description:通过向从“源树”中拖拽结点在目标树中重新构造一棵树,实现JTree之间的拖拽单向拖拽,这个类是“源树”。 </p>

* @author awaysrain(绝对零度)

* @version 1.0

*/

public class DragSourceTree

extends JTree

implements DragGestureListener, DragSourceListener {

private DragSource dragSource = null;

/**

* Construct the source tree

*/

public DragSourceTree() {

super();

dragSource = DragSource.getDefaultDragSource();

dragSource.createDefaultDragGestureRecognizer(this,

java.awt.dnd.DnDConstants.

ACTION_COPY_OR_MOVE, this);

}

/**

* Construct the source tree

* @param treeNode DefaultMutableTreeNode

*/

public DragSourceTree(DefaultMutableTreeNode treeNode) {

super(treeNode);

dragSource = DragSource.getDefaultDragSource();

dragSource.createDefaultDragGestureRecognizer(this,

java.awt.dnd.DnDConstants.

ACTION_COPY_OR_MOVE, this);

}

/**

* dragGestureRecognized

*

* @param dge DragGestureEvent

*/

public void dragGestureRecognized(DragGestureEvent dge) {

java.awt.Cursor cursor = null;

cursor = DragSource.DefaultCopyDrop; //set cursor

java.awt.Point jap = dge.getDragOrigin(); //drag point

int x = (int) jap.getX();

int y = (int) jap.getY();

javax.swing.tree.TreePath tp = this.getPathForLocation(x, y); //get drag node path

if (tp != null) {

DefaultMutableTreeNode node1 = (DefaultMutableTreeNode)

tp.getLastPathComponent(); //get drag node

serializableNode(node1); //Serializable node object

}

//event start

dge.startDrag(cursor, new StringSelection("drag"), this);

}

/**

* dragEnter

*

* @param dsde DragSourceDragEvent

*/

public void dragEnter(DragSourceDragEvent dsde) {

}

/**

* dragOver

*

* @param dsde DragSourceDragEvent

*/

public void dragOver(DragSourceDragEvent dsde) {

}

/**

* dropActionChanged

*

* @param dsde DragSourceDragEvent

*/

public void dropActionChanged(DragSourceDragEvent dsde) {

}

/**

* dragDropEnd

*

* @param dsde DragSourceDropEvent

*/

public void dragDropEnd(DragSourceDropEvent dsde) {

}

/**

* dragExit

*

* @param dse DragSourceEvent

*/

public void dragExit(DragSourceEvent dse) {

}

/**

*Serializable node

*

* @param node DefaultMutableTreeNode

*/

private void serializableNode(DefaultMutableTreeNode node) {

try {

java.io.FileOutputStream objfile = new java.io.FileOutputStream(

"DragNode.OBJ");

java.io.ObjectOutputStream p = new java.io.ObjectOutputStream(objfile);

p.writeObject(node);

p.flush();

objfile.close();

}

catch (java.io.IOException e) {

System.out.println(e.toString());

}

}

}

DragTargetTree.java

package aabbcc;

import javax.swing.JTree;

import java.awt.dnd.DropTargetListener;

import java.awt.dnd.DropTargetDragEvent;

import java.awt.dnd.DropTargetDropEvent;

import java.awt.dnd.DropTargetEvent;

import java.awt.dnd.DropTarget;

import java.io.*;

import java.io.*;

import javax.swing.tree.DefaultMutableTreeNode;

/**

* <p>Title: JTree之间的拖拽中的DragTargetTree</p>

* <p>Description:通过向从“源树”中拖拽结点在目标树中重新构造一棵树,实现JTree之间的拖拽单向拖拽,这个类是“目的树” </p>

* @author awaysrain(绝对零度)

* @version 1.0

*/

public class DragTargetTree

extends JTree

implements DropTargetListener {

/**

* Construct the target tree

*/

public DragTargetTree() {

super();

DropTarget dropTarget = new DropTarget(this,

java.awt.dnd.DnDConstants.

ACTION_COPY_OR_MOVE, this);

}

/**

* Construct the target tree

* @param treeNode DefaultMutableTreeNode

*/

public DragTargetTree(DefaultMutableTreeNode treeNode) {

super(treeNode);

DropTarget dropTarget = new DropTarget(this,

java.awt.dnd.DnDConstants.

ACTION_COPY_OR_MOVE, this);

}

/**

* dragEnter

*

* @param dtde DropTargetDragEvent

*/

public void dragEnter(DropTargetDragEvent dtde) {

}

/**

* dragOver

*

* @param dtde DropTargetDragEvent

*/

public void dragOver(DropTargetDragEvent dtde) {

}

/**

* dropActionChanged

*

* @param dtde DropTargetDragEvent

*/

public void dropActionChanged(DropTargetDragEvent dtde) {

}

/**

* drop

*

* @param dtde DropTargetDropEvent

*/

public void drop(DropTargetDropEvent dtde) {

dtde.acceptDrop(java.awt.dnd.DnDConstants.ACTION_COPY); //accept drop

java.awt.Point ap = dtde.getLocation(); // get event point

int x = (int) ap.getX();

int y = (int) ap.getY();

javax.swing.tree.DefaultMutableTreeNode parnode;

javax.swing.tree.TreePath tp = this.getPathForLocation(x, y);

if (tp != null) {

try {

//get drop tree node with treepath

javax.swing.tree.DefaultMutableTreeNode no1 =

(javax.swing.tree.DefaultMutableTreeNode) tp.getLastPathComponent();

parnode = no1;

int index = parnode.getChildCount();

// get drop tree mode

javax.swing.tree.DefaultTreeModel tm =

(javax.swing.tree.DefaultTreeModel)this.getModel();

javax.swing.tree.DefaultMutableTreeNode node2 =

new javax.swing.tree.DefaultMutableTreeNode();

node2 = readNode();

tm.insertNodeInto(node2, parnode, index);

}

catch (java.lang.Exception e) {

System.out.println(e.toString());

}

}

}

/**

* dragExit

*

* @param dte DropTargetEvent

*/

public void dragExit(DropTargetEvent dte) {

}

/**

*

* read the node

* @return DefaultMutableTreeNode

*/

private DefaultMutableTreeNode readNode() {

FileInputStream objfile = null;

try {

objfile = new java.io.FileInputStream("DragNode.OBJ");

ObjectInputStream q = new ObjectInputStream(objfile);

DefaultMutableTreeNode node = (DefaultMutableTreeNode) q.readObject();

return node;

}

catch (Exception ex) {

return null;

}

}

}

Demo.java

package aabbcc;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import com.borland.jbcl.layout.*;

/**

* <p>Title: JTree之间的拖拽测试</p>

* <p>Description:通过向从“源树”中拖拽结点在目标树中重新构造一棵树,实现JTree之间的拖拽单向拖拽,这个类测试的类 </p>

* @author awaysrain(绝对零度)

*/

public class Demo extends JFrame {

JPanel contentPane;

JScrollPane jScrollPane1 = new JScrollPane();

XYLayout xYLayout1 = new XYLayout();

DragSourceTree jTree1 = new DragSourceTree();

JScrollPane jScrollPane2 = new JScrollPane();

DragTargetTree jTree2 = new DragTargetTree();

JLabel jLabel1 = new JLabel();

JLabel jLabel2 = new JLabel();

//Construct the frame

public Demo () {

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

//Component initialization

private void jbInit() throws Exception {

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(xYLayout1);

this.setSize(new Dimension(400, 300));

this.setTitle("Demo");

jLabel1.setFont(new java.awt.Font("DialogInput", 0, 16));

jLabel1.setText("源树");

jLabel2.setEnabled(true);

jLabel2.setFont(new java.awt.Font("DialogInput", 0, 16));

jLabel2.setDebugGraphicsOptions(0);

jLabel2.setDoubleBuffered(false);

jLabel2.setVerifyInputWhenFocusTarget(true);

jLabel2.setDisplayedMnemonic('0');

jLabel2.setText("目标树");

jLabel2.setVerticalAlignment(javax.swing.SwingConstants.CENTER);

jLabel2.setVerticalTextPosition(javax.swing.SwingConstants.CENTER);

contentPane.add(jLabel1, new XYConstraints(35, 8, 139, 27));

contentPane.add(jLabel2, new XYConstraints(214, 10, 149, 20));

contentPane.add(jScrollPane2, new XYConstraints(211, 31, 159, 231));

contentPane.add(jScrollPane1, new XYConstraints(25, 31, 153, 230));

jScrollPane1.getViewport().add(jTree1, null);

jScrollPane2.getViewport().add(jTree2, null);

}

//Overridden so we can exit when window is closed

protected void processWindowEvent(WindowEvent e) {

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {

System.exit(0);

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有