关于JAVA树
我想了解JTree的详细使用方法 最好有点案例的 ,谢谢
[解决办法]
/** * 在光标退出放置组件的显示区时发生 * @param e DragSourceEvent */ public void dragExit(DragSourceEvent e) {} /** * 在光标进入放置组件显示区之后移动时调用 * @param e DragSourceDragEvent */ public void dragOver(DragSourceDragEvent e) {} /** * 选择放置操作的修饰键的状态变化 * @param e DragSourceDragEvent */ public void dropActionChanged(DragSourceDragEvent e) {} /** *放置发生并调用DropTargetListener的drop()方法后,调用dragDropEnd()方法, * 告诉拖动源放置已经完成 * @param e DragSourceDropEvent */ public void dragDropEnd(DragSourceDropEvent e) { //getDropSuccess()对应DropTargetListener的drop()方法调用dropcomplete()时指目标指定的值 //getDropAction()对应DropTargetListener的drop()方法调用acceptDrop()时指定的操作 if (!e.getDropSuccess()||e.getDropAction()!= DnDConstants.ACTION_MOVE) { return; } DragSourceContext context = e.getDragSourceContext(); Object comp = context.getComponent(); if (comp==null||!(comp instanceof JTree)) { return ; } DefaultMutableTreeNode dragTreeNode = getDragTreeNode(); if (dragTreeNode!=null) { ((DefaultTreeModel) tree.getModel()).removeNodeFromParent(dragTreeNode); //设置拖动的树节点为空 setDragTreeNode(null); } } } public static void main(String[] args) { new Frame1().setVisible(true); } } class Frame1_jButton2_actionAdapter implements ActionListener { private Frame1 adaptee; Frame1_jButton2_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed(e); } } class Frame1_jButton1_actionAdapter implements ActionListener { private Frame1 adaptee; Frame1_jButton1_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); } }
[解决办法]
DragAndDropDropTargetListener.java
package tree; import java.awt.Component; import java.awt.datatransfer.Transferable; import java.awt.dnd.*; import javax.swing.JTree; import javax.swing.tree.*; //* Drop Target Listener */ public class DragAndDropDropTargetListener implements DropTargetListener { //e.rejectDrop()只可以在dragEnter()、dragOver()和dropActionChanged()中调用,不能在drop()中调用 /** * 在光标进入放置组件的显示区时调用 * @param e DropTargetDragEvent */ public void dragEnter(DropTargetDragEvent e) { } /** * 在光标进入放置组件显示区之后移动时调用 * @param e DropTargetDragEvent */ public void dragOver(DropTargetDragEvent e) { } /** * 选择放置操作的修饰键的状态变化 * @param e DropTargetDragEvent */ public void dropActionChanged(DropTargetDragEvent e) {} /** * 在光标退出放置组件的显示区时发生 * @param e DropTargetEvent */ public void dragExit(DropTargetEvent e) {} /** * 在发生放置时调用,负责接受或拒绝放置请求和处理放置的数据 * @param e DropTargetDropEvent */ public void drop(DropTargetDropEvent e) { //获取要传递的数据 Transferable transfer = e.getTransferable(); DefaultMutableTreeNode dragSource = null; //是否支持树节点数据的传递 if (transfer.isDataFlavorSupported(DragAndDropTransferable.TREENODE_FLAVOR)) { try { //设置接受移动的操作 e.acceptDrop(DnDConstants.ACTION_MOVE); //获取传递的数据 dragSource = (DefaultMutableTreeNode) transfer.getTransferData( DragAndDropTransferable.TREENODE_FLAVOR); } catch (Exception ex) { ex.printStackTrace(); } } if (dragSource == null) { //拖动的数据源为空则退出 //放置不成功 e.dropComplete(false); return; } //获取dropTo对象 DropTarget dt = (DropTarget) e.getSource(); Component comp = dt.getComponent(); if (!(comp instanceof JTree)) { //放置不成功 e.dropComplete(false); return; } JTree jtr = (JTree) comp; TreePath treePath = jtr.getPathForLocation(e.getLocation().x,e.getLocation().y); if (treePath==null) { //放置不成功 e.dropComplete(false); return; } DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); if (!isCanDrop(dragSource,treeNode,jtr)) { //放置不成功 e.dropComplete(false); return; } //把节点添加到当前树节点下 treeNode.add(dragSource); //更新树节点 ((DefaultTreeModel)jtr.getModel()).reload(treeNode); //设置放置成功 e.dropComplete(true); } /** * 判断是否可以放置操作 * @param dragTreeNode DefaultMutableTreeNode 拖动源的树节点 * @param dropTreeNode DefaultMutableTreeNode 放置目标的树节点 * @return boolean */ public boolean isCanDrop(DefaultMutableTreeNode dragTreeNode, DefaultMutableTreeNode dropTreeNode, JTree jtr) { if (dragTreeNode == null) { //拖动源为空则退出 return false; } //设置放置目标为空时不可放置 if (dropTreeNode == null ) { return false; } //放置目标是拖动源则退出 if (dragTreeNode==dropTreeNode) { return false; } TreePath dragPath = new TreePath (((DefaultTreeModel )jtr.getModel()).getPathToRoot(dragTreeNode)); TreePath dropPath = new TreePath (((DefaultTreeModel )jtr.getModel()).getPathToRoot(dropTreeNode)); String strDragPath = dragPath.toString(); String strDropPath = dropPath.toString(); String subDragPath = strDragPath.substring(0,strDragPath.length()-1); if (strDropPath.startsWith(subDragPath)) {//放置目标是拖动源的子孙节点 return false; } if ( dragPath.getParentPath().toString().equals(strDropPath)) {//放置目标是拖动源的父节点 return false; } //放置目标是拖动源的父节点则退出 if (dragTreeNode.getParent().equals(dropTreeNode)) { return false; } return true; } }
[解决办法]
DragAndDropTransferable.java
package tree; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; import javax.swing.tree.DefaultMutableTreeNode; /**/ /* Drop Transferable */ public class DragAndDropTransferable implements Transferable { private DefaultMutableTreeNode treeNode; //??????????DataFlavor public final static DataFlavor TREENODE_FLAVOR = new DataFlavor(DefaultMutableTreeNode.class,"TreeNode instance"); public DataFlavor[] flavors = new DataFlavor[] {TREENODE_FLAVOR}; public DragAndDropTransferable(DefaultMutableTreeNode treeNode) { this.treeNode = treeNode; } public DataFlavor[] getTransferDataFlavors() { return flavors; } public boolean isDataFlavorSupported(DataFlavor flavor) { for (DataFlavor df : flavors) { if (df.equals(flavor)) { return true; } } return false; } public Object getTransferData(DataFlavor df) throws UnsupportedFlavorException, IOException { if (df.equals(TREENODE_FLAVOR)) { return treeNode; } throw new UnsupportedFlavorException(df); } }
[解决办法]
5楼部分代码缺失,6楼帮他补上。