Perform drag&drop in Jtree without even selecting a node once?

Hi All,
Is it possible to perform drag and Drop action on tree nodes even when the Jtree is not active.
That is, say a jtree is not active and i directly want to drag a node inside it without even selecting it once?
Thanks alot in Advance.

You should google for "jtree drag and drop". I remember seeing a very nice tutorial on codeproject.
If your run into any specific issue let me know.

Similar Messages

  • Drag & Drop from JTree (String) to JTextArea (Graphics2D)

    Hi everyone,
    I want to create a Drag & Drop from a JTree node to any location in a JTextArea. Upon dropping the node onto the JTextArea, I would want to draw a rectangle and the node name.
    I've created a custom transfer handler which allows the transferring of text from the tree node to the JTextArea.
    Any general ideas on how I can do that are wlecomed.

    I've managed to get hold of the JPanel via TransferHandler.TransferSupport.getComponent()
    the source seems a bit more tricky. I've tried this
    Transferable tr = support.getTransferable();
                   DataFlavor f = new DataFlavor();
                   f = new DataFlavor(MyTreeNode.class,"MyTreeNode");
                   try {
                        Object o = tr.getTransferData(f);...
    but the source does not provide a MyTreeNode instance, but something else (a String, I guess)
    how can I change this?

  • Drag&drop columns no longer works for selected columns in EA3

    Hi
    drag&drop from navigator to build select statements in EA3 uses all columns of the tabl instead of the selected ones!
    Best regards
    Joe

    It's working for me in our current dev build, if it's broken in EA3, it's since been fixed.

  • Tree drag-drop function

    hello all,
    there is one single tree component in my application.
    XML is dataprovider of that tree.
    e.g
    <node name="Cars" id="1" isparent="true">
    <node name="Audi" id="2" isparent="true">
    <node name="A4" id="3" isparent="false"/>
    <node name="A6" id="4" isparent="false"/>
    </node>
    <node name="Mercedes" id="5" isparent="true">
    <node name="M-Class" id="6" isparent="false"/>
    <node name="R-Class" id="7" isparent="false"/>
    </node>
    </node
    when i drag one node and drop to another node, how can i know the xml value of
    both source & destination node?
    e.g
    we are Dragging 'R-Class' node to 'Audi' node, on dragComplete OR dragDrop
    event, how to get R-Class node id 7 & Audi node id 2 in different variables.
    So we can check some validation of node and perform drag-drop operation.
    Any idea?
    Steve Roger.

    actually what i want to implement is like :
    When drapDrop event complete, i want XML value of both nodes (source & target) in variables.
    Then I will show dialog box having some options & instructions (e.g You wanna Move node OR Copy node to target).
    Depend on user selection, do the operation.
    hope u get application scenario.
    is this possible for tree control using drag drop? if so, then how?
    Please help.
    Thank you,
    Steve Roger.

  • Can't use drag&drop

    Hello, I'm using MacBook Pro (retina, 15", late 2013, 2,3 GHz Intel core i7) and after using for a while happened smh strange. I can't use drag&drop, so I can't select the object and drag it. What is wrong?

    Hello!
    I wonder, does the issue persist even after a simple restart of OS X?
    If so, please start Disk Utility (it's in your Applications>Utilities folder), click on your harddrive and select [repair disk permissions]

  • Captivate8: Drag&Drop not possible in a responsive design?

    Is it possible to create a Drag&Drop Interaction within a responsive design? (Captivate 8)
    I can't open the Drag&Drop Menu, I can't select it.

    I've also completed the form to request new features. But in the meantime could I ask how people get around this?
    We have several courses created for desktop computers which include drag and drop tasks, and feedback from learners is that they would like to be able to access them on mobile devices. So as I understand it we have three options:
    Create separate courses for mobile devices, using responsive design but losing the drag and drop tasks. This would mean we have two versions of each course, one for desktop and one for mobile, which may cause issues with updating, etc, and the mobile user won't get quite the same learning experience as they'll lose the drag and drop tasks.
    Adapt the main desktop course so it is responsive, and lose the drag and drop altogether. This has the advantage of everyone seeing the same course and it only needing updating once.
    Don't use responsive design and display the course in the same way on a desktop as on a tablet. This may be fine for larger screen tablets, but wouldn't be great for smaller tablets or phones as you'd need to do a lot of zooming and scrolling.
    I'm just wondering what you all do to get around it?

  • Multiple Selection in JTree, Drag & Drop

    Hi everybody,
    I was wondering if anyone knows how to implement Multiple Selection Drag and Drop in a JTree. I know that there used to be a bug in jdk 1.3 that would allow it, but I suspect 1.4 fixed that. I've implemented Single-selection just fine, but I'm trying do Multiple selection by drawing a selection rectangle and selecting whatever nodes are contained within that rectangle. So far I've implemented it by adding a MouseListener to my JTree, finding which rows are intersected by the rectangle and calling addSelectionRow() to add the TreePaths.
    Is there an easier way to multiple selection now that the bug of 1.3(I hope) has been fixed?

    Okay, now I've implemented the changes in the DragGestureListener, DragSourceListener and DragTargetListener in order to make Multiple Selection possible, however I want to select multiple items the same way Windows Explorer does by:
    1. pressing the left-mouse button & dragging a "selection" rectangle which selects all the nodes contained within that rectangle.
    2. then drag and drop is accomplished by pressing one of the selected nodes and dragging it (along with it's other selected siblings) to the destination.
    In order to accomplish this I added into the Constructor of my JTree extending class this code:
    public class MyTree extends JTree {
    private static int x1, y1, x2, y2;
    public MyTree(DefaultMutableTreeNode rootNode){
    // Other Constructor stuff...
    this.addMouseMotionListener( new MouseMotionListener() {
    public void mouseDragged(MouseEvent e) {  
    x2 = e.getX();
    y2 = e.getY();
    repaint();
    public void mouseMoved(MouseEvent e){     }
    this.addMouseListener(new MouseAdapter(){
    boolean dontRelease = false;
    public void mousePressed( MouseEvent e){
    x1 = e.getX();
    y1 = e.getY();
    int[] rows = getSelectionRows();
    boolean dropSel = true;
    if (rows != null){
    for (int i = 0; i < rows.length; i++){
    Rectangle rect3 = getRowBounds(i);
    if (rect3.contains(x1, y1)){
    dropSel = false;
    dontRelease = true;
    if (dropSel){
    for (int i =0; i < rows.length; i++)
    removeSelectionRow(i);
    public void mouseReleased( MouseEvent e){
    x2 = e.getX();
    y2 = e.getY();
    repaint();
    if (dontRelease == false) {
    Rectangle rect2 = new Rectangle(Math.min(x1, x2),
    Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
    int rowCount = getRowCount();
    for (int i = 1; i < rowCount; i++) {
    Rectangle rect3 = getRowBounds(i);
    if ((rect2.contains(rect3)) || (rect2.intersects(rect3))){
    addSelectionRow(i);
    dontRelease = false;
    repaint();
    The idea was that for every press of the mouse the 1st set of x,y coords. are taken to be the starting corner of the "selection" rectangle, and every point passed over while dragging would be the ending corner of the rectangle (so that in paintComponents, you could actually draw the rectangle).
    Once the mouse is released it will check to see if the selection rectangle had contained or intersected any of the rows (basically I find how many rows are being shown in the tree and go through each one to see if there is intersection/containment). If a row is contained it becomes selected.
    The remaining code in MousePressed is used if node(s) were previously selected it goes through and checks to see if this mouse press is at the location of one of the previous selected nodes - if it is then you can drag that node along with all of its siblings to their destination. However if your mouse press is not on one of the selected nodes - all the nodes should be deselected (like in explorer if you press away from your selection, you lose it). If you select and begin to drag your selection dontRelease should not allow the creation of another "selection" rectangle to occur during the drag-n-drop process.
    -That's how I thought this code should work. Unfortunately as it does most of these things it does not correctly implement rule #2 of Windows Explorer (2. only by dragging one of the selected nodes can you drag them, otherwise you lose them)...currently as long as something is selected you can do a mouse press and drag from another location and as long as that location does not contain a node (like it is a blank area in the JTree), you can drag everything to the destination from when you start dragging.
    I looked around and saw that perhaps it is due to the BasicTreeUI interferring with it's own MouseInputHandler() so I wrote MyTreeUI that extends BasicTreeUI, basically changing only the MouseInputHandler to contain the functions above, and I blocked /**/ those functions from my MyTree Constructor.
    Then in my main class (the one of instantiate my MyTree and my DefaultMutableTreeNodes), I wrote:
    try
    String myTreeUI = "MyTreeUI";
    UIManager.put( "MyTreeUI", myTreeUI );
    UIManager.put( myTreeUI,
    Class.forName( myTreeUI ) );
    } catch( ClassNotFoundException cnfe ) {
    System.err.println( "My Tree UI class not found" );
    System.err.println( cnfe );
    ...thinking that that would be all I needed in order to set MyTreeUI as the JTree's UI, LooknFeel, etc. and although I did not get that ClassNotFoundException when running the program, it did not have any of the functionality that I had written ( I even wrote a System.out.println() in my MousePressed() function in MyTreeUI so that it would let me know that it is working and it never appeared). It was as if the MyTreeUI was not loaded or attached to the JTree. I also called:
    System.out.println("this is UI: " + jTree1.getUIClassID() );
    to see which UI my MyTree instantiate was using and it would printout TreeUI, not MyTreeUI. So I'm wondering what it is I'm doing wrong, I think I am very close to having this thing working but I want to know if anybody here would be able to let me know what I'm doing wrong or what would be the best way to go about implementing this Windows Explorer Multiple Selection JTree.
    Sorry for the long-winded explanation,
    -Meddev24.

  • HT1267 i've got about 2500 songs on my iphone, i have replaced my computer & lost my library-question is why can't i drag/drop new downloaded music to my iphone without 'syncing' & losing my 2500 songs from the iphone?

    I've got about 2500 songs on my iphone, i have recently replaced my computer because the old one crashed, so i lost all the songs in the itunes library. My question is, it seems that now i am unable to drag/drop new downloaded songs without syncing my iphone & losing my 2500 songs from the phone?

    You cannot. Iphone will sync/manually manage music with one and only one computer at a time.  Syncing/manually managing with another will erase the current content from the iphone.
    It has always been very basic to always maintain a backup copy of your computer.  Use your backup copy to put everything back.

  • Drag and Drop Learning Interaction without Overlapping

    I would like to be able to drag and drop into a column without dragged items overlapping for a quiz question. I don't know Action Script and am using CS3. What do I need to do to get this to work. Component Inspector states Drag1 = Target 1, etc. I need to drag the items to any of targets 1, 2, and 3. Hopefully, someone out there can help me.
    Thank you.
    Laraine

    Thank you for your reply.  Your idea of a boolean variable to define the state of the response is what I was initially trying to accomplish.  Almost as soon as clicked on 'submit', the thought occurred to me to look for action script in the drag/drop component.  I found it and found the response states defined there.  Consequently, I achieved external movie clip control.  I'm new to this forum and appreciate your help.

  • Multiple Selection on JTree without holding down [CTRL] or [SHIFT]

    I need an example about how make multiple selection on JTree without holding down [CTRL] or [SHIFT].
    my JTree contains JCheckBox in any nodes, but I can't select two or more checkBox in time without holding down [CRTL] or [SHIFT].
    thanks for help.
    Jose A.

    I did this a few years ago so my newbiness is going to show through a bit, but I'm too lazy to update it.import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import java.util.*;
    public class Test3 extends JFrame {
      JTree jt = new JTree();
      MultiTreeSelectionModel mtsm = new MultiTreeSelectionModel(jt);
      JCheckBox multiCheck = new JCheckBox("Multi"),
          branchCheck = new JCheckBox("Branch");
      public Test3() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel content = (JPanel)getContentPane();
        multiCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mtsm.setMultiSelect(multiCheck.isSelected());
        branchCheck.setText("Branch");
        branchCheck.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
                mtsm.setBranchSelect(branchCheck.isSelected());
        JPanel jp = new JPanel();
        jp.add(multiCheck);
        jp.add(branchCheck);
        content.add(jp, BorderLayout.NORTH);
        ActionListener specialKeyListener =
            new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    MultiTreeSelectionModel.keyModifiers = evt.getModifiers();
        KeyStroke keyStroke;
        for (int i = 0; i < keys.length; i++) {
            keyStroke = KeyStroke.getKeyStroke(keys[0], 0, true);
    content.registerKeyboardAction(specialKeyListener, keyStroke,
    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    keyStroke = KeyStroke.getKeyStroke(keys[i][0], keys[i][1], false);
    content.registerKeyboardAction(specialKeyListener, keyStroke,
    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    jt.setSelectionModel(mtsm);
    content.add(new JScrollPane(jt), BorderLayout.CENTER);
    setSize(300, 300);
    setVisible(true);
    public static void main(String[] args) { new Test3(); }
    private static int[][] keys = {{KeyEvent.VK_CONTROL, ActionEvent.CTRL_MASK},
    {KeyEvent.VK_SHIFT, ActionEvent.SHIFT_MASK},
    {KeyEvent.VK_ALT, ActionEvent.ALT_MASK}};
    class MultiTreeSelectionModel extends DefaultTreeSelectionModel {
    static int keyModifiers;
    private boolean branchSelect, multiSelect;
    private JTree tree;
    private TreePath[] savePaths;
    MultiTreeSelectionModel(JTree Tree) {
    tree = Tree;
    setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    private boolean isSelected(TreePath Path) {
    return tree.isPathSelected(Path) && (keyModifiers & KeyEvent.SHIFT_MASK) == 0;
    private boolean branchSelect() {
    return branchSelect || ((keyModifiers & KeyEvent.ALT_MASK) != 0);
    public void addSelectionPaths(TreePath[] paths) {
    if (branchSelect()) paths = getAllPaths(paths);
    super.addSelectionPaths(paths);
    public void removeSelectionPaths(TreePath[] paths) {
    if (branchSelect()) paths = getAllPaths(paths);
    super.removeSelectionPaths(paths);
    public void setSelectionPaths(TreePath[] paths) {
    if (branchSelect()) {
    paths = getAllPaths(paths);
    if (paths != null && paths.length > 0 && isSelected(paths[0])) {
    super.removeSelectionPaths(paths);
    } else if (multiSelect) super.addSelectionPaths(paths);
    else super.setSelectionPaths(paths);
    protected TreePath[] getAllPaths(TreePath[] paths) {
    if (paths == null || paths.length == 0) {
    return paths;
    Vector vector = new Vector();
    DefaultMutableTreeNode treeNode, thisNode;
    for (int i = 0; i < paths.length; i++) {
    if (paths[i] != null) {
    thisNode = (DefaultMutableTreeNode) paths[i].getLastPathComponent();
    Enumeration enumeration = thisNode.preorderEnumeration();
    while (enumeration.hasMoreElements()) {
    // add all descendants to vector
    treeNode = (DefaultMutableTreeNode) enumeration.nextElement();
    TreePath treePath = new TreePath(treeNode.getPath());
    vector.add(treePath);
    int i = vector.size();
    TreePath[] allpaths = new TreePath[i];
    for (int j = 0; j < i; j++) {
    allpaths[j] = (TreePath) vector.elementAt(j);
    return allpaths;
    protected void setMultiSelect(boolean b) { multiSelect = b; }
    protected boolean isMultiSelect() { return multiSelect; }
    protected void setBranchSelect(boolean b) { branchSelect = b; }
    protected boolean isBranchSelect() { return branchSelect; }
    protected void savePaths(TreePath Path) {
    TreePath[] tmpPaths = getSelectionPaths();
    if (tmpPaths == null) {
    savePaths = null;
    } else {
    int cnt = 0;
    for (int i = 0; i < tmpPaths.length; i++) {
    if (tmpPaths[i].isDescendant(Path)) {
    cnt++;
    savePaths = new TreePath[cnt];
    cnt = 0;
    for (int i = 0; i < tmpPaths.length; i++) {
    if (tmpPaths[i].equals(Path) || tmpPaths[i].isDescendant(Path)) {
    savePaths[cnt++] = tmpPaths[i];
    protected void restorePaths() {
    if (savePaths != null) {
    final DefaultTreeSelectionModel foo = this;
    SwingUtilities.invokeLater(
    new Runnable() {
    public void run() {
    foo.setSelectionPaths(savePaths);

  • I wolud like to make a Drag&Drop JTree

    Hi everyone:
    I'm trying to implement a JTree with Drag&Drop support. I've been lookig for some code examples but It was imposible to find them. I didn't found either information about drag&drop.
    Thank you very much. Chefo

    Captivate is not really designed to do what you want in this instance.  It's more targeted at allowing a user to complete an assessment and then track whether or not they passed.  So it's only really set up to send information in a format that the LMS will understand and interpret as Pass/Faill and what the score was.
    You really need something more flexible than that. So I think you would need to get a programmer involved and have the solution custom made.  In my experience, any time you try to diverge Captivate from what it was designed to do, and you need to get other IT professionals involved, you can kiss thousands of dollars goodbye before you have a workable solution.

  • select Tag Mozilla cuts off at 20 IE6 cuts off at 30 We are expecting 30 more lines in drop down list without scrollbar.

    <select> Tag Mozilla cuts off at 20 IE6 cuts off at 30 We are expecting 30 more lines in drop down list without scrollbar.

    it has been answered

  • Drag & Drop without copy across collections

    When I drag & drop photos from one collection to another, selected photos are left in the original collection and also added to the other one. Often I would like to actually move the photos from one collection to another, therefore I would find it very convenient to be able to do it with a modifier key during drag & drop (e.g. Ctrl on a PC).

    I agree with this request. I've also found moving images between collections unwieldy.

  • Mulit task selection and drag drop

    Hi Gurus,
    SAP cProjects UI does not have capability of Drag Drop and multi task selections in the project strucuture tree.
    I have narrowed down two options which include customisations.
    If any one knows simpler options it would be a great help.
    Thanks in advance.
    Niketan.

    Hi,
    > Drag & Drop and multi task selections in the project strucuture tree
    will be available with "SAP Portfolio and Project Management 5.0" (the successor of cProjects 4.5 and SAP RPM 4.5).
    Kind regards,
       Florian

  • How to select node in JTree without firing event?

    I have got standard situation. JTree in the left panel, and several edit boxes in right panel. Certainly, I have TreeSelectionListener, which on every tree node selection shows corresponding model values in edit boxes.
    I'd like to implement next logic:
    1. Something was changed in any edit box in right panel. Model has been changed.
    2. User clicks on different node in tree.
    3. Dialog "Message was not saved. Save?" with Yes/No/Cancel buttons are shown.
    Yes/No buttons are easy to handle.
    Question is about Cancel. I'd like on Cancel button left all edit boxes in their present state (do not restore values from saved model) and select back node, which wasn't saved.
    Problem is next. If I select node by setSelectionPath or smth like that, but... JTree fires event and my listener receives onTreeItemSelected back, which checks that node wasn't saved and ......
    Who does have any idea, or have done similar tasks? How can I select node and do not allow tree fire event this time?
    Thanks in advance.

    First, as soon as the model changes (when editing any
    combo box) some flag will be set. Now the logic which
    updates the combo boxes will do it only on a change of
    the current node and (this is new) if the flag wasn't
    set. You should have some flag anyway because somehow
    you must determine when to show the dialog, shouldn't
    you?Yes, I have got this logic implemented. But it's only the half :)
    I know exactly when my model has been changed, but if it was changed, i'd like to ask user what to do next - svae/loose changes/cancel
    And on cancel i'd like to select last edited tree node and do not get event from tree at that moment.
    >
    Second way, prevent selecting a new node if that flag
    has been set. You could do this by subclassing
    DefaultTreeSelectionModel and overriding some methods
    (setSelectionPath() et al).Ok. I'll investigate this.
    >
    MichaelThanks.

Maybe you are looking for