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 n drop Jtree


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 53
    Par défaut Drag n drop Jtree
    Bonjour ,
    j'ai reussi a faire un drag n drop sur un jTree mais voila je trouve que le code est compliquer et je n'arrive pas a avoir une cue line entre les nodes. Est ce que quelqu'un pourrai m'éclairé sur ce sujet.

  2. #2
    Membre émérite

    Inscrit en
    Décembre 2004
    Messages
    584
    Détails du profil
    Informations forums :
    Inscription : Décembre 2004
    Messages : 584
    Par défaut
    As tu vu le cours qui vient de paraitre sur le sujet :
    Introduction au Drag and Drop : transfert de données
    http://mbaron.developpez.com/javase/dnddt/ ?

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 53
    Par défaut
    Oui mais jtree ne gere pas le MOVE par default et je voudrai pouvoir classer mes differentes nodes avec une ligne qui s'intercale sur le meme jtree.
    En fait je cherche a utiliser le on_or_insert avec jtree.setDrag(true);
    http://java.sun.com/javase/6/docs/ap...l#ON_OR_INSERT
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
                            jTree.setDragEnabled(true);
    			jTree.setDropMode(DropMode.ON_OR_INSERT);
    			jTree.setTransferHandler(new NewTransferHandler());
    http://weblogs.java.net/blog/shan_ma...class_dra.html
    Est ce que c'est possible ? Comment faire pour que mon TransferHandler accepte
    le on_or_insert ?

  4. #4
    Membre averti
    Inscrit en
    Novembre 2006
    Messages
    31
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 31
    Par défaut
    Bonjour CristalWing,

    La fonctionnalité DropMode apparait avec la java 6. Comme je travaille encore en java 5 je ne peux pas t'aider sur cette fonctionnalité. Mais je fais la même chose avec peu de code. Si ça peut t'aider ...

    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
    private Rectangle rect = new Rectangle();
     
    public boolean canPerformAction(JTree tree, DefaultMutableTreeNode draggedNode, int action, Point location) {
        TreePath pathTarget = tree.getPathForLocation(location.x, location.y);
        if (draggedNode.isRoot()) {
            tree.setSelectionPath(null);
        } else if (pathTarget != null) {
            // clear previous insertion line
            tree.paintImmediately((int) rect.getX(),(int) rect.getY(),(int) rect.getWidth() + 1,(int) rect.getHeight() + 1);
            Rectangle rectTarget = tree.getPathBounds(pathTarget);
            DefaultMutableTreeNode targetNode = (DefaultMutableTreeNode)pathTarget.getLastPathComponent();
            // show new insertion line
            if (rectTarget.getCenterY() < location.y) {
                rect.setBounds((int) rectTarget.getX(),(int) (rectTarget.getY() + rectTarget.getHeight()) - 1,(int) rectTarget.getWidth(),1);
                tree.getGraphics().drawRect((int) rect.getX(),(int) rect.getY(),(int) rect.getWidth(),(int) rect.getHeight());
            } else {
                 rect.setBounds((int) rectTarget.getX(),(int) rectTarget.getY() - 1,(int) rectTarget.getWidth(),1);
                 tree.getGraphics().drawRect((int) rect.getX(),(int) rect.getY(),(int) rect.getWidth(),(int) rect.getHeight());
            }
        }
        return true;
    }
     
    public void dragDropEnd(DragSourceDropEvent dsde) {
            tree.paintImmediately(tree.getBounds());
    }
    Je pense que ce code est à insérer dans ta classe NewTransferHandler.

    A+

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 53
    Par défaut
    Merci patanoc , ton code va me depanner le temps que je trouve l'implementation java 6, si elle est possible, d'ailleur si quelqu'un connait ces nouvautées je suis preneur.

    A+

  6. #6
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 53
    Par défaut
    J'ai essayer de faire l'implementation java 6 comme dans le cours dnd mais je me demande comment exclure certaines nodes avec un panneau sens interdit (isAllowsChildren())
    J'ai egalement un probleme de null.Pointer.Exception
    Voila mon exception

    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
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at javax.swing.plaf.basic.BasicTreeUI.getDropLineRect(Unknown Source)
    	at javax.swing.plaf.basic.BasicTreeUI.access$1700(Unknown Source)
    	at javax.swing.plaf.basic.BasicTreeUI$Handler.repaintDropLocation(Unknown Source)
    	at javax.swing.plaf.basic.BasicTreeUI$Handler.propertyChange(Unknown Source)
    	at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source)
    	at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source)
    	at java.awt.Component.firePropertyChange(Unknown Source)
    	at javax.swing.JTree.setDropLocation(Unknown Source)
    	at javax.swing.TransferHandler$DropHandler.setComponentDropLocation(Unknown Source)
    	at javax.swing.TransferHandler$DropHandler.cleanup(Unknown Source)
    	at javax.swing.TransferHandler$DropHandler.drop(Unknown Source)
    	at java.awt.dnd.DropTarget.drop(Unknown Source)
    	at javax.swing.TransferHandler$SwingDropTarget.drop(Unknown Source)
    	at sun.awt.dnd.SunDropTargetContextPeer.processDropMessage(Unknown Source)
    	at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchDropEvent(Unknown Source)
    	at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchEvent(Unknown Source)
    	at sun.awt.dnd.SunDropTargetEvent.dispatch(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processDropTargetEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    et mon code :
    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
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.Transferable;
    import java.awt.dnd.DropTargetListener;
     
    import javax.swing.JComponent;
    import javax.swing.JTree;
    import javax.swing.TransferHandler;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreePath;
     
    import data.object.TreeObjectTools;
     
    public class NewTransferHandler extends TransferHandler {
     
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
    	private DefaultMutableTreeNode _transferedNode = null;
    	private JTree _tree = null;
    	private DefaultMutableTreeNode _targetDropNode = null;
    	public NewTransferHandler(){
    		super();
     
    	}
     
    	public boolean canImport(JComponent c, DataFlavor[] d) {
    		boolean ret = false;
    		for (int i = 0; i < d.length; i++) {
    			if (d[i].equals(TransferableNodeEX.NODE_FLAVOR)) {	
    				ret = true;
    			}
    		}
    		return ret;	
    	}
     
    	public boolean importData(JComponent c, Transferable t) {
    		//DefaultMutableTreeNode node=null;
    		_tree = (JTree)c;
    		if (t.isDataFlavorSupported(TransferableNodeEX.NODE_FLAVOR)) {
    			try {
    				Object myNode = t.getTransferData(TransferableNodeEX.NODE_FLAVOR);
    				_transferedNode = (DefaultMutableTreeNode)myNode;
     
    				//System.out.println(myNode.toString());
     
    				TreePath pathTarget = _tree.getDropLocation().getPath();
    				_targetDropNode =(DefaultMutableTreeNode)pathTarget.getLastPathComponent();
    				int index = _tree.getDropLocation().getChildIndex();
     
    				if (index == -1){
    					index = 0;
    				}
    				System.out.println(index);
    				if (!_targetDropNode.getAllowsChildren()|| _transferedNode.isNodeDescendant(_targetDropNode)){
    					return false;
     
    				}else {
    					((DefaultTreeModel)_tree.getModel()).insertNodeInto(_transferedNode, _targetDropNode, index );
    					return true;
    				}
     
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
     
    		}
    		return false;
     
    	}
     
    	protected Transferable createTransferable(JComponent c) {
    		DefaultMutableTreeNode transferedNode=null;
    		if (c!=null){
    			JTree tree = (JTree)c;
    			transferedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent() ;
    			//System.out.println(_transferedNode.toString());
    		}
    			return new TransferableNodeEX(transferedNode);
    	}
     
    	protected void exportDone(JComponent c, Transferable t, int type) {
    			if (type == TransferHandler.MOVE) {
     
    				//((DefaultTreeModel)tree.getModel()).removeNodeFromParent(TransferedNode);
     
    				//System.out.println(t.toString());
    			} else if (type == TransferHandler.COPY) {
     
    			}
    	}
     
    	public int getSourceActions(JComponent cp) {
    		return COPY_OR_MOVE;
     
    	}
     
     
     
    }
    Si quelqu'un a une idée pour le sens interdit et pour cette exception qui apparai lors de certain dnd avec les cue line

    Voila si quelqu'un peut m'aider a y voir plus clair ca serai sympa

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Drag and drop jtree
    Par jose.hello dans le forum Composants
    Réponses: 6
    Dernier message: 28/05/2010, 11h30
  2. Drag and Drop Jtree sous jdk 1.5
    Par iRonny dans le forum Composants
    Réponses: 3
    Dernier message: 01/04/2009, 11h10
  3. Drag and Drop Jtree
    Par reno_tidus dans le forum Composants
    Réponses: 14
    Dernier message: 26/09/2007, 10h17
  4. Drag And Drop Jtree
    Par mkl78 dans le forum Composants
    Réponses: 3
    Dernier message: 29/06/2007, 13h40
  5. Drag n drop / JTree / remove
    Par snico dans le forum Composants
    Réponses: 3
    Dernier message: 24/03/2007, 21h57

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