IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants Java Discussion :

Drag and drop sur un JTree


Sujet :

Composants Java

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    167
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 167
    Points : 120
    Points
    120
    Par défaut Drag and drop sur un JTree
    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.

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    390
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 390
    Points : 432
    Points
    432
    Par défaut
    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

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    167
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 167
    Points : 120
    Points
    120
    Par défaut
    Merci bien, je regarde ca!

  4. #4
    Membre régulier
    Inscrit en
    Décembre 2004
    Messages
    226
    Détails du profil
    Informations forums :
    Inscription : Décembre 2004
    Messages : 226
    Points : 102
    Points
    102
    Par défaut
    il me semble que cet article en parle aussi

    http://www.javaworld.com/javaworld/j...javatip97.html

  5. #5
    Membre confirmé Avatar de billynirvana
    Homme Profil pro
    Architecte technique
    Inscrit en
    Décembre 2004
    Messages
    472
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Décembre 2004
    Messages : 472
    Points : 552
    Points
    552
    Par défaut
    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()));
      }

Discussions similaires

  1. Drag and drop sur une PictureBox
    Par abdiouldbody dans le forum VB 6 et antérieur
    Réponses: 4
    Dernier message: 17/06/2009, 15h55
  2. Drag and drop sur une listbox (access 2002)
    Par puballenou dans le forum IHM
    Réponses: 1
    Dernier message: 23/11/2006, 17h57
  3. drag and drop sur une listBox?
    Par Mickey.jet dans le forum Delphi
    Réponses: 3
    Dernier message: 30/09/2006, 10h27
  4. Drag and Drop sur une JTree
    Par Xhéras dans le forum Composants
    Réponses: 5
    Dernier message: 07/07/2006, 12h09
  5. Drag and drop sur du text ?
    Par isa150183 dans le forum JSF
    Réponses: 2
    Dernier message: 05/07/2006, 06h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo