JTree Drag and Drop (Multi Nodes)

Hi All!
I have been searching the forums for a few hours over the last few days and have found some nice examples and explanations on how to implement drag and drop for a jtree.
However, I would love to know how this is accomplished when selecting multiple nodes.
I want to be able to see all the selected nodes being dragged...
Anyone implemented anything of the sort... or even just knows how to do this?
Thanks in advance!!
Aaron.

When you drag get the [x,y] mouse position and use the JTree method:
getRowForLocation(x, y) for node you at the also for the parent node location of the child node.
Once the childY is less than childParentY, you stop the drag.
Hope that helps?

Similar Messages

  • How to drag and drop tab nodes between tab panes

    I'm working on example from this tutorial( Drag-and-Drop Feature in JavaFX Applications | JavaFX 2 Tutorials and Documentation ). Based on the tutorial I want to drag tabs between two tabs. So far I managed to create this code but I need some help in order to finish the code.
    Source
    tabPane = new TabPane();
    Tab tabA = new Tab();
       Label tabALabel = new Label("Main Component");
    tabPane.setOnDragDetected(new EventHandler<MouseEvent>()
                @Override
                public void handle(MouseEvent event)
                    /* drag was detected, start drag-and-drop gesture*/
                    System.out.println("onDragDetected");
                    /* allow any transfer mode */
                    Dragboard db = tabPane.startDragAndDrop(TransferMode.ANY);
                    /* put a string on dragboard */
                    ClipboardContent content = new ClipboardContent();
                    content.put(DataFormat.PLAIN_TEXT, tabPane);
                    db.setContent(content);
                    event.consume();
    What is the proper way to insert the content of the tab as object? Into the tutorial simple text is transferred. How I must modify this line content.put(DataFormat.PLAIN_TEXT, tabPane);?
    And what is the proper way to insert the tab after I drag the tab:
    Destination
    tabPane.setOnDragDropped(new EventHandler<DragEvent>()
                @Override
                public void handle(DragEvent event)
                    /* data dropped */
                    /* if there is a string data on dragboard, read it and use it */
                    Dragboard db = event.getDragboard();
                    boolean success = false;
                    if (db.hasString())
                        //tabPane.setText(db.getString());
                        Tab tabC = new Tab();
                        tabPane.getTabs().add(tabC);
                        success = true;
                    /* let the source know whether the string was successfully
                     * transferred and used */
                    event.setDropCompleted(success);
                    event.consume();
    I suppose that this transfer can be accomplished?
    Ref javafx 2 - How to drag and drop tab nodes between tab panes - Stack Overflow

    I would use a graphic (instead of text) for the Tabs and call setOnDragDetected on that graphic. That way you know which tab is being dragged. There's no nice way to put the Tab itself into the dragboard as it's not serializable (see https://javafx-jira.kenai.com/browse/RT-29082), so you probably just want to store the tab currently being dragged in a property.
    Here's a quick example; it just adds the tab to the end of the current tabs in the dropped pane. If you wanted to insert it into the nearest location to the actual drop you could probably iterate through the tabs and figure the coordinates of each tab's graphic, or something.
    import java.util.Random;
    import javafx.application.Application;
    import javafx.beans.property.ObjectProperty;
    import javafx.beans.property.SimpleObjectProperty;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.Tab;
    import javafx.scene.control.TabPane;
    import javafx.scene.input.ClipboardContent;
    import javafx.scene.input.DragEvent;
    import javafx.scene.input.Dragboard;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.input.TransferMode;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    public class DraggingTabPane extends Application {
      private static final String TAB_DRAG_KEY = "tab" ;
      private ObjectProperty<Tab> draggingTab ;
    @Override
      public void start(Stage primaryStage) {
      draggingTab = new SimpleObjectProperty<>();
      TabPane tabPane1 = createTabPane();
      TabPane tabPane2 = createTabPane();
      VBox root = new VBox(10);
      root.getChildren().addAll(tabPane1, tabPane2);
      final Random rng = new Random();
      for (int i=1; i<=8; i++) {
        final Tab tab = createTab("Tab "+i);
        final StackPane pane = new StackPane();
          int red = rng.nextInt(256);
          int green = rng.nextInt(256);
          int blue = rng.nextInt(256);
        String style = String.format("-fx-background-color: rgb(%d, %d, %d);", red, green, blue);
        pane.setStyle(style);
        final Label label = new Label("This is tab "+i);
        label.setStyle(String.format("-fx-text-fill: rgb(%d, %d, %d);", 256-red, 256-green, 256-blue));
        pane.getChildren().add(label);
        pane.setMinWidth(600);
        pane.setMinHeight(250);
        tab.setContent(pane);
        if (i<=4) {
          tabPane1.getTabs().add(tab);
        } else {
          tabPane2.getTabs().add(tab);
      primaryStage.setScene(new Scene(root, 600, 600));
      primaryStage.show();
      public static void main(String[] args) {
      launch(args);
      private TabPane createTabPane() {
        final TabPane tabPane = new TabPane();
        tabPane.setOnDragOver(new EventHandler<DragEvent>() {
          @Override
          public void handle(DragEvent event) {
            final Dragboard dragboard = event.getDragboard();
            if (dragboard.hasString()
                && TAB_DRAG_KEY.equals(dragboard.getString())
                && draggingTab.get() != null
                && draggingTab.get().getTabPane() != tabPane) {
              event.acceptTransferModes(TransferMode.MOVE);
              event.consume();
        tabPane.setOnDragDropped(new EventHandler<DragEvent>() {
          @Override
          public void handle(DragEvent event) {
            final Dragboard dragboard = event.getDragboard();
            if (dragboard.hasString()
                && TAB_DRAG_KEY.equals(dragboard.getString())
                && draggingTab.get() != null
                && draggingTab.get().getTabPane() != tabPane) {
              final Tab tab = draggingTab.get();
              tab.getTabPane().getTabs().remove(tab);
              tabPane.getTabs().add(tab);
              event.setDropCompleted(true);
              draggingTab.set(null);
              event.consume();
        return tabPane ;
      private Tab createTab(String text) {
        final Tab tab = new Tab();
        final Label label = new Label(text);
        tab.setGraphic(label);
        label.setOnDragDetected(new EventHandler<MouseEvent>() {
          @Override
          public void handle(MouseEvent event) {
            Dragboard dragboard = label.startDragAndDrop(TransferMode.MOVE);
            ClipboardContent clipboardContent = new ClipboardContent();
            clipboardContent.putString(TAB_DRAG_KEY);
            dragboard.setContent(clipboardContent);
            draggingTab.set(tab);
            event.consume();
        return tab ;

  • How drag and drop a node in a TreeView ?

    Hi,
    I want drag and drop a node in a treeview but i don't know how to get this result. I can drag a page item in a treeview or drag a node in a page item but not a node in another.
    Can you help me ?
    Regards,
    Damien Fontaine

    Your implementation of DoAddDragContent() is a little thin looking.
    If you look at the example in the SDK (BscDNDCustomDragSource.cpp) you might need to add at least:
        // we get the data object that represents the drag
        InterfacePtr<IPMDataObject> item(DNDController->AddDragItem(1));
        // no flavor flags
        PMFlavorFlags flavorFlags = 0;
        // we set the type (flavour) in the drag object
        item->PromiseFlavor(customFlavor, flavorFlags);
    Also, check that DoAddDragContent()  is actually being called.

  • Drag and Drop different nodes within a JTree

    I have a JTree that represents a hierarchical object. Within the tree are different nodes representing different objects, each within a container,
    For Example:
    A
    -B container
    -- B NodeObject
    -- B NodeObject
    -C Container
    -- C NodeObject
    -- C NodeObject
    I have got the drag and drop working but as the gesture listener is on the actual JTree object it allows me to drag any object to any other place.
    What i want to be able to do is to force the drag and drop to allow only objects of type C to be put in the C container, etc and to reject any moves
    over other containers.
    As TreeNodes are not Components how do i force this behaviour?
    If i have to i can force the behaviour after the mouse has been released but I would prefer it if Swing would show the correct mouse over icons
    when I put an object over the wrong container.
    Thanks
    Dan Alford

    When you drag get the [x,y] mouse position and use the JTree method:
    getRowForLocation(x, y) for node you at the also for the parent node location of the child node.
    Once the childY is less than childParentY, you stop the drag.
    Hope that helps?

  • JTree Drag and Drop question

    I have drag and drop working fairly well using jdk 1.4. I'm able to copy and move objects around in the tree and change the cursor as it moves over various objects in the tree. The only problem I'm having is when the user drops something into someplace that rejects the drop, for instance in the blank area of the JTree object. In this case, the drop never seems to complete. If I try to drag that same object again it throws an InvalidDnDOperationException saying that a DnD operation is already in progress. I noticed that the drop() operation is not being called when the user drops the object into a rejected area.
    I've searched the forums and haven't seen anyone else report this problem. Does anyone have any suggestions?

    I'd love it too if you would send me you code, or post it so others can benefit; there seems to be quite a number of people who want DnD JTree functionality. I've been going nuts trying to figure out the best way to implement DnD on a JTree. I started using the new TransferHandler and had reasonable success with it. But, I'm not sure how to get the cursor to change based on DropTarget (not all nodes are created equal in this regard). I need access to the DragSourceContext, but I don't know how to get it when using the TransferHandler method.
    I have other issues with the TransferHandler also, like trying to make DnD work consistently with Cut-And-Paste all in the same TransferHandler. Or, trying to deal with the fact that you can change a move-type DnD operation into a copy-type operation after you start dragging, but before you drop just by pressing the control-key. This sort of implies to me that the original createTransferable() should always make a copy, so that the only issue is whether to clean up in exportDone().

  • How to drag and drop multi tabs to a new tab group?

    When I go to the new feature Tab Groups (shift+ctrl+E), I make a search then some tabs are highlighted but I would like to drag and drop to a new group the highlighted tabs all together not just one by one. Is it possible doing a multi-drag&drop with the CTRL+left mouse button?

    I see where you're going. I'm drawn to Thunderbird because it puts open emails in separate tabs, which makes finding a particular open message easier than searching the task bar for an open outlook message. I do see your point that tab happy programmers, probably too young to remember how cool we thought drag and drop was when it first came out, don't think much of it. As with a lot of software, each programmer has a very limited view of how it their program should be used vs how it is actually used in the wild.
    People work in different ways. When I schedule an appointment with a client, this is often preceded by a series of emails related to whatever the client wants to discuss. I drag and drop those messages into the appointment window so that on the date of the appointment, I have a nice neat summary of the issues right there in the appointment window. I could use workarounds (save email and then attach to the appointment), but that's like a trip back to Windows 3.0 and it's, if I'm not mistaken, 30+ years later. I guess I'll continue to use Tbird for email and outlook for calendar, not my preferred solution, I'd like to flip MS the bird and cut all ties to outlook, and was hoping Tbird was the solution. Not yet, and perhaps not ever, based on your thoughts.

  • JTree drag and drop

    Hi.. How can I drag and drop items from one JTree to another??? thanks!!!

    The following link might help you :
    http://forum.java.sun.com/thread.jspa?forumID=57&threa
    dID=296255Thanks! this thread has been useful. How can I manipulate the code so that I can have two different trees instead of just one tree? How can I make the drag and drop one-way???

  • Drag and drop a node in the line graph

    Hello Flex experts,
      I am using line graph for the monthly data. I want to drag a node , maybe the february node down. How can i achieve it? My code as follows does not work. Will highly appreciate your help as it is urgent requirement.
    <?xml version="1.0"?><!-- charts/BasicLine.mxml --><mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script><![CDATA[ 
    import mx.controls.Alert; 
    import mx.collections.ArrayCollection; 
    import mx.core.DragSource; 
    import mx.managers.DragManager; 
    import mx.events.*; 
    import mx.containers.Canvas;
    private  
    function dragExitHandler(event:DragEvent):void {Alert.show(
    " An event exit . ");}
    private  
    function dragOverHandler(event:DragEvent):void {Alert.show(
    " An event drag . ");}
    Bindable] 
    public var expenses:ArrayCollection = new ArrayCollection([{Month:
    "Jan", Profit:2000, Expenses:1500, Amount:450},{Month:
    "Feb", Profit:1000, Expenses:200, Amount:600},{Month:
    "Mar", Profit:1500, Expenses:500, Amount:300}]);
    ]]>
    </mx:Script>
    <mx:Panel title="Line Chart">
    <mx:LineChart id="myChart" dataProvider="
    {expenses}" showDataTips="
    true"dragEnabled="
    true"dragMoveEnabled="
    true"dropEnabled="
    true"dragOver="dragOverHandler(event);"
    dragDrop="dragExitHandler(event);"
    dragExit="dragExitHandler(event);"
    >
    <mx:horizontalAxis>
    <mx:CategoryAxis dataProvider="
    {expenses}" categoryField="
    Month" 
    />
    </mx:horizontalAxis>
    <mx:series>
    <mx:LineSeries yField="
    Profit" displayName="
    Profit" 
    />
    <mx:LineSeries yField="
    Expenses" displayName="
    Expenses" 
    />
    </mx:series>
    </mx:LineChart>
    <mx:Legend dataProvider="{myChart}"/>
    </mx:Panel> 
    Best regards!
    Sandip

    did you ever figure this out? I am trying to figure this out too, and it's not easy.

  • Drag and Drop nodes in JTree

    Is there a way to drap and drop JTree nodes without resorting to hacks? http://www.javaworld.com/javatips/jw-javatip97.html outlines a hack to get drag and drop working but it is kinda old. I am wondering whether there is a now a way to do this in Swing. http://weblogs.java.net/blog/shan_man/archive/2006/01/first_class_dra.html outlines a seemingly promising way but when I tried it it still wouldn't let me drag JTree nodes. (btw, if you are going to try it, you must replace TransferHandler.TransferInfo with TransferHandler.TransferSupport. It has apprently been renamed.)

    I have implemented drag and drop of nodes in JTree they same way it is explained in JavaWorld link that you mentioned, I customized it for my application and it worked great.

  • Drag and Drop between JTree and Labels in 2 different panes

    Hi,
    I am using JDK 1.4.1 and am trying to implement Drag n Drop between a JTree and JLabels with ImageIcons, both on different panes.
    I got the DnD working fine on the tree. but i cant get to drag and drop the label from the other pane, on the Jtree as a node. I am getting confused with the DataFlavors, and also wonder if there is something else that i have to do for DnD between 2 panes. Can someone give me any leads on this, please? The Panes I am talking about are splitpanes.
    thanks,
    Sri.

    hey thanks Dennis!! I was hoping you would respond to my question, as I have seen a lot of your replies. yes, the example you gave would be helpful. but i am trying to implement this in 1.4.1, with the new drag and drop in swing. and i am getting confused wiht theh data flavors etc.
    my problem at hand is :
    I have a tree on the left pane. i can drag and drop the nodes on the tree itself. (i already did that...no problem). I have Jlabels with imageicons (actually wrapper classes with labels and imageicons) on the right pane. i have to be able to drag these labels to the tree such that they form a node.
    I have one class the NodeSelection class which extends TransferHandler and implements Transferable. i was able to do DnD for the tree using this. I customized the importData method in this.I thought that i can just use the same class to create the transferhandler and transferable, and just use a different implementation for the importData method if the data being dragged is from the label.
    well, my thoughts and ideas were ok...but i can implement it...so they must be wrong ;-)
    Can u please suggest another way to do this. I have a deadline to meet and am stuck here :-( any suggestions or tips or hints would be very helpful and appreciated!!
    Thanks,
    Sri.

  • Drag and drop the xml schema?

    hi all
    i am creating a GUI. i am using splitpane to split my Frame. and my left split pane contains the XML schema in the form of a JTree. and i need to drag and drop the nodes of a JTree on to right split pane which sub divided in to some regions. i can able to open the xml file in to left split pane as a Jtree. but unable to drag and drop the Nodes in to rightsplit pane.
    any e book or sample code is greatly appriciated.
    thanks in advance.
    karthik

    Hi
    I am new to Oracle but not to VS. I have downloaded and installed the newest tools for Developer, having first tried the install for VS2003.net (which refused to install on my VS 2005.) I got the beta download and installed it, but my oracle explorer shows nothing. Any attempt to add a connection results in an error ORA-12560:TNS:protocol adaptor error. I had the full install of 10g r2 on my notebook, but removed it and opted for the XE version. I removed the Oracle_Home environment variable prior to installing XE. I am trying to connect to the HR database, and the account is unlocked. I saw a reference to the tnsfiles file, and that seems to be the problem, but I don't know what to do about it. Thanks in advance to anyone who can offer help.
    Bruce
    [email protected]

  • Drag and Drop between 2 Panes

    Hi,
    I am using JDK 1.4.1 and am trying to implement Drag n Drop between a JTree and Labels with ImageIcons, both on different panes.
    I got the DnD working just on the tree. but i cant get to drag and drop the label on the Jtree as a node. I am getting confused with the DataFlavors, and also wonder if there is something else that i have to do for DnD between 2 panes. Can someone give me any leads on this, please?
    thanks,
    Sri.

    hey thanks Dennis!! I was hoping you would respond to my question, as I have seen a lot of your replies. yes, the example you gave would be helpful. but i am trying to implement this in 1.4.1, with the new drag and drop in swing. and i am getting confused wiht theh data flavors etc.
    my problem at hand is :
    I have a tree on the left pane. i can drag and drop the nodes on the tree itself. (i already did that...no problem). I have Jlabels with imageicons (actually wrapper classes with labels and imageicons) on the right pane. i have to be able to drag these labels to the tree such that they form a node.
    I have one class the NodeSelection class which extends TransferHandler and implements Transferable. i was able to do DnD for the tree using this. I customized the importData method in this.I thought that i can just use the same class to create the transferhandler and transferable, and just use a different implementation for the importData method if the data being dragged is from the label.
    well, my thoughts and ideas were ok...but i can implement it...so they must be wrong ;-)
    Can u please suggest another way to do this. I have a deadline to meet and am stuck here :-( any suggestions or tips or hints would be very helpful and appreciated!!
    Thanks,
    Sri.

  • Dragging ande dropping into a frame?

    Hi all,
    I am new to Swing Programming and i have a problem in implementing the JTree.
    According my requirement i need to drag a child node of the Jtree and drop into a frame which is adjacent to the JTree pane.
    But i am struck at only dragging and dropping the child node inside the tree itself,
    Please help me by some links or tutorials or any help is greatly appreciated.
    Thanks In advance.....
    Looking forward for your reply.....
    Thanks again,
    karthik.

    now i can able to compile the source code.thanks for that but my actual problem is i am creating a GUI. i am using splitpane to split my Frame. and my left split pane contains the XML schema in the form of a JTree. and i need to drag and drop the nodes of a JTree on to right split pane which sub divided in to some regions. i can able to open the xml file in to left split pane as a Jtree. but unable to drag and drop the Nodes in to rightsplit pane.

  • Drag and Drop - Drag Image

    I'm using the Flex DragManager in an AdvancedDataGrid with
    HierarchicalData to allow the user to drag and drop tree nodes to
    re-order them. It is working fine. The problem I am having is with
    the drag image. The drag image does not always show up. It seems
    like it will appear for one drag and drop. Then if I try to drag
    another tree node I do not get the drag image there is just a line
    to show I'm dragging something.
    When I say drag image I mean the faded image of the text of
    the tree node which appears as I drag the tree node.
    I am using the following properties on the AdvancedDataGrid
    to enable drag and drop:
    dragEnabled="true" dropEnabled="true" dragMoveEnabled="true"
    Now if set dragMoveEnabled="false" then I do get a drag image
    everytime. But I need move the tree nodes around not copy them so I
    need dragMoveEnabled="true".
    Thank you

    I'm using the Flex DragManager in an AdvancedDataGrid with
    HierarchicalData to allow the user to drag and drop tree nodes to
    re-order them. It is working fine. The problem I am having is with
    the drag image. The drag image does not always show up. It seems
    like it will appear for one drag and drop. Then if I try to drag
    another tree node I do not get the drag image there is just a line
    to show I'm dragging something.
    When I say drag image I mean the faded image of the text of
    the tree node which appears as I drag the tree node.
    I am using the following properties on the AdvancedDataGrid
    to enable drag and drop:
    dragEnabled="true" dropEnabled="true" dragMoveEnabled="true"
    Now if set dragMoveEnabled="false" then I do get a drag image
    everytime. But I need move the tree nodes around not copy them so I
    need dragMoveEnabled="true".
    Thank you

  • Display JTree Node as image  while Drag And Drop

    HI all.
    I have multiple 5 JTrees which involve drag and drop to each other . I would like to display the node(JCompenent) as image/cursor which a DnD operation .So that user could see what he drags .
    I tried out the example about ghost image .
    http://www.javaworld.com/javaworld/javatips/jw-javatip114.html
    This works fine ,but when i apply the same concepts to my application it doesnt work .
    Could any one suggest what could be problem.
    Cheers,Baiju

    I also need to drag my node on my scene during a "drag and drop" operation. But i can't find a solution.
    I've tried to use "startFullDrag()" and catch MouseDragEvent, but nothing ...
    Can anyone help me ?
    Thank !

Maybe you are looking for

  • How can I split my itunes account?

    We have had an itunes account for both of my teenage boys.  Now that one is off to college and the other is into different music we find it very complicated to have 2 different accounts.  Is there any way to keep items they purchased on this account

  • IPhone not recognised in My Computer, but is in itunes

    About a week ago my iPhone just stopped being recognised in My Computer. Before this it was fine, it would recognise and I could move ohotos from my phone to an external hardrive. All of a sudden its not showing up there though? It is recognised in i

  • UWL Configuration

    Hello Gurus, I am facing the flowwing problem in Portal working with UWL. In Universal Worklist system configuration, when I'm trying to create new system connector for UWL "MdmUwlConnector" is not showing up in that list. After that I restart the se

  • Page is too small to read

    I was playing farmville. It sent me to a page, when I tried to get out the whole page shrunk and the words are almost impossible to read. There used to be a place on the bottom right screen, where it showed percentages of the screen size: 50% 85% etc

  • Copy clip markers to new project?

    I have a project file that was used by the director to make descriptive markers on clips. it references identical media and it was just a basic project file dupe for the director to use. i'd like to copy the markers in his project and add those to my