Bonjour, je souhaite mettre en place un système de drag and drop sur un Jtree....Quelqu'un aurait-il des pistes pour réaliser ceci?
Merci d'avance.
Bonjour, je souhaite mettre en place un système de drag and drop sur un Jtree....Quelqu'un aurait-il des pistes pour réaliser ceci?
Merci d'avance.
Regarde les classes :
DragSource
DropTarget
Et les interfaces :
DragSourceListener
DropTargetListener
DragGestureListener
Transferable
Tu auras besoin de tout ca.
Et si tu n'est pas refractaire à l'anglais tu as des exemples sur le site de Sun:
http://java.sun.com/products/jfc/tsc...rop/index.html
Voila avec ca tu as tout ce qu'il faut
il me semble que cet article en parle aussi![]()
http://www.javaworld.com/javaworld/j...javatip97.html
Voilà par un exemple (une partie de mon code partiel) comment je procèd
. Pose des questions si tu ne comprends pas certains trucs.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 public class Fenetre extends JInternalFrame implements ActionListener, DropTargetListener, DragSourceListener, DragGestureListener, InternalFrameListener, KeyListener, MouseListener, TreeSelectionListener { private JScrollPane scrollPane; public JTree tree; private DropTarget dropTarget = null; private DragSource dragSource = null; private DefaultMutableTreeNode selNode = null; public DefaultMutableTreeNode tmpNode = null; private DefaultMutableTreeNode dropNode = null; ... }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public Fenetre(JFrame parent, String nom, int x, int y) { super(nom, true, false, true, true); this.setFrameIcon(null); this.parent = parent; tree = new JTree(...); tree.setToolTipText(""); // laisser sinon ca marche pas! tree.setLargeModel(true); tree.setCellRenderer(new myTreeCellRenderer()); tree.addTreeSelectionListener(this); tree.addKeyListener(this); tree.addMouseListener(this); dropTarget = new DropTarget(tree, this); dragSource = new DragSource(); dragSource.createDefaultDragGestureRecognizer(tree, DnDConstants.ACTION_MOVE, this); MouseListener popupListener = new PopupListener(this); tree.addMouseListener(popupListener); scrollPane = new JScrollPane(tree); this.getContentPane().add(scrollPane); this.setSize(250, 300); this.setLocation(x, y); this.setVisible(true); this.addInternalFrameListener(this); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 ///////////////////////////////////////////// // DRAG & DROP public void dragEnter(DropTargetDragEvent e) { e.acceptDrag(DnDConstants.ACTION_MOVE); } public void dragGestureRecognized(DragGestureEvent e) { selNode = null; dropNode = null; Object selected = tree.getSelectionPath(); if (selected != null) { TreePath treepath = (TreePath) selected; selNode = (DefaultMutableTreeNode) treepath.getLastPathComponent(); dragSource.startDrag(e, DragSource.DefaultMoveDrop, new StringSelection(selected.toString()), this); } } public void drop(DropTargetDropEvent e) { Transferable transferable = e.getTransferable(); if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) { e.acceptDrop(DnDConstants.ACTION_MOVE); Point dropPoint = e.getLocation(); TreePath dropPath = tree.getClosestPathForLocation(dropPoint.x,dropPoint.y); dropNode = (DefaultMutableTreeNode) dropPath.getLastPathComponent(); e.getDropTargetContext().dropComplete(true); } else { e.rejectDrop(); } } public void dragDropEnd(DragSourceDropEvent e) { if (e.getDropSuccess()) { if (dropNode == null) System.out.println(getTitle() + ": " + "Deplacement impossible car vous n'êtes pas dans le même arbre!"); else if (isNoeudDansPath(dropNode, selNode)) System.out.println(getTitle() + ": " + "Deplacement impossible car le noeud source est un parent du noeud de destination!"); else if (dropNode == selNode) { System.out.println(getTitle() + ": " + "Deplacement impossible car le noeud source et le noeud de destination sont les mêmes!"); } else { if (!isNoeudElement(dropNode)) dropNode = (DefaultMutableTreeNode) dropNode.getParent(); ((DefaultTreeModel) tree.getModel()).removeNodeFromParent(selNode); ((DefaultTreeModel) tree.getModel()).insertNodeInto(selNode, dropNode, dropNode.getChildCount()); ouvrir(dropNode); } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 public boolean isNoeudParent(DefaultMutableTreeNode node) { return (tree.isRootVisible() && tree.getRowForPath(new TreePath(node.getPath())) == 0); } public void ouvrir(DefaultMutableTreeNode node) { if (node == null) return; if (node.isLeaf() && !isNoeudParent(node)) node = (DefaultMutableTreeNode) node.getParent(); tree.expandPath(new TreePath(node.getPath())); }
Partager