0 Replies Latest reply: Feb 26, 2013 11:50 AM by 993489 RSS

    Not able to insert node after the last node in the leaf

    993489
      hi

      i try to drag a component to a jtree, i'm not able to add a node after the last node of a leaf.

      With the code below,
      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.GridLayout;
      import java.awt.event.ItemEvent;
      import java.awt.event.ItemListener;
      import java.awt.event.MouseAdapter;
      import java.awt.event.MouseEvent;
      import javax.swing.DropMode;
      import javax.swing.JComboBox;
      import javax.swing.JComponent;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTextField;
      import javax.swing.JTree;
      import javax.swing.TransferHandler;
      import javax.swing.tree.DefaultMutableTreeNode;
      import javax.swing.tree.DefaultTreeModel;
      import javax.swing.tree.TreeModel;
      import testglob.MyTransferHandler;
      
      public class TreeDragDemo extends JFrame {
      
          JTree tree;
      
          public TreeDragDemo() {
              setTitle("Drag'n Drop with label!");
              setSize(400, 200);
              setLocationRelativeTo(null);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
              JPanel pan = new JPanel();
              pan.setLayout(new GridLayout(1, 1));
              pan.setBackground(Color.white);
      
              JLabel srcLib = new JLabel("Drag Source : ", JLabel.RIGHT);
              JLabel src = new JLabel("Noeud 1");
      
              src.setTransferHandler(new MyTransferHandler());
              src.addMouseListener(new MouseAdapter() {
                  public void mousePressed(MouseEvent e) {
                      JComponent lab = (JComponent) e.getSource();
                      TransferHandler handle = lab.getTransferHandler();
                      handle.exportAsDrag(lab, e, TransferHandler.MOVE);
                  }
              });
      
              tree = new JTree(getModel());
              tree.setDragEnabled(true);
              tree.setTransferHandler(new TreeTransferHandler(tree));
      
              pan.add(src);
      
              pan.add(new JScrollPane(tree));
      
              JComboBox combo = new JComboBox();
              combo.addItem("USE_SELECTION");
              combo.addItem("ON");
              combo.addItem("INSERT");
              combo.addItem("ON_OR_INSERT");
      
              combo.addItemListener(new ItemListener() {
                  public void itemStateChanged(ItemEvent event) {
                      String value = event.getItem().toString();
      
                      if (value.equals("USE_SELECTION")) {
                          tree.setDropMode(DropMode.USE_SELECTION);
                      }
      
                      if (value.equals("ON")) {
                          tree.setDropMode(DropMode.ON);
                      }
      
                      if (value.equals("INSERT")) {
                          tree.setDropMode(DropMode.INSERT);
                      }
      
                      if (value.equals("ON_OR_INSERT")) {
                          tree.setDropMode(DropMode.ON_OR_INSERT);
                      }
      
                  }
              });
      
              add(pan, BorderLayout.CENTER);
              add(combo, BorderLayout.SOUTH);
              setVisible(true);
          }
      
          private TreeModel getModel() {
      
              DefaultMutableTreeNode root = new DefaultMutableTreeNode("SDZ");
      
              DefaultMutableTreeNode forum = new DefaultMutableTreeNode("Forum");
              forum.add(new DefaultMutableTreeNode("C++"));
              forum.add(new DefaultMutableTreeNode("Java"));
              forum.add(new DefaultMutableTreeNode("PHP"));
      
              DefaultMutableTreeNode tuto = new DefaultMutableTreeNode("Tutorial");
              tuto.add(new DefaultMutableTreeNode("Tutorial"));
              tuto.add(new DefaultMutableTreeNode("Programmation"));
              tuto.add(new DefaultMutableTreeNode("Mapping"));
      
              root.add(tuto);
              root.add(forum);
      
              return new DefaultTreeModel(root);
          }
      
          public static void main(String[] args) {
              new TreeDragDemo();
          }
      }
      import java.awt.datatransfer.DataFlavor;
      import java.awt.datatransfer.StringSelection;
      import java.awt.datatransfer.Transferable;
      import java.awt.datatransfer.UnsupportedFlavorException;
      import java.io.IOException;
      import javax.swing.JComponent;
      import javax.swing.JLabel;
      import javax.swing.TransferHandler;
      
      public class MyTransferHandler extends TransferHandler {
      
          public boolean canImport(TransferHandler.TransferSupport info) {
              if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                  return false;
              }
              return true;
          }
      
          public boolean importData(TransferHandler.TransferSupport support) {
              if (!canImport(support)) {
                  return false;
              }
      
              Transferable data = support.getTransferable();
              String str = "";
      
              try {
                  str = (String) data.getTransferData(DataFlavor.stringFlavor);
              } catch (UnsupportedFlavorException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
      
              JLabel lab = (JLabel) support.getComponent();
              lab.setText(str);
      
              return false;
          }
      
          protected void exportDone(JComponent c, Transferable t, int action) {
              if (action == MOVE) {
                  JLabel lab = (JLabel) c;
                  String text = lab.getText();
                  int indice = Integer.parseInt(text.substring(text.length() - 1, text.length()));
                  lab.setText(text.substring(0, text.length() - 1) + (++indice));
              }
          }
      
          protected Transferable createTransferable(JComponent c) {
              return new StringSelection(((JLabel) c).getText());
          }
      
          public int getSourceActions(JComponent c) {
              return MOVE;
          }
      }
      import java.awt.datatransfer.DataFlavor;
      import java.awt.datatransfer.Transferable;
      import java.awt.datatransfer.UnsupportedFlavorException;
      import java.io.IOException;
      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;
      
      public class TreeTransferHandler extends TransferHandler {
      
          JTree tree;
      
          public TreeTransferHandler(JTree tree) {
              this.tree = tree;
          }
      
          public boolean canImport(TransferHandler.TransferSupport info) {
              if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                  return false;
              }
              return true;
          }
      
          public boolean importData(TransferHandler.TransferSupport support) {
              if (!canImport(support)) {
                  return false;
              }
      
              JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
      
              TreePath path = dl.getPath();
              int index = dl.getChildIndex();
      
              //get data
              Transferable data = support.getTransferable();
              String str = "";
      
              try {
                  str = (String) data.getTransferData(DataFlavor.stringFlavor);
              } catch (UnsupportedFlavorException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
      
      
              DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(str);
      
              DefaultMutableTreeNode parent = (DefaultMutableTreeNode) path.getLastPathComponent();
      
              DefaultTreeModel model = (DefaultTreeModel) this.tree.getModel();
              index = (index == -1) ? model.getChildCount(path.getLastPathComponent()) : index;
              model.insertNodeInto(newNode, parent, index);
      
              tree.makeVisible(path.pathByAddingChild(newNode));
              tree.scrollPathToVisible(path);
      
              return true;
          }
      
          public int getSourceActions(JComponent c) {
              return COPY_OR_MOVE;
          }
      }
      if i try to add a node below PHP in the leaf Forum or below Mapping in the leaf Tutorial, the node will be inserted to the root.

      i tried with different mode: use_selection, on, insert and on_or_insert

      Any idea?