Drag in TreeView

Hi all

I want to make a simple request where you have a tree and on the other side three pictures. In this example I used three rectangles each with a different color.

The user should be able to drag one of the rectangles of an item in the tree in order to add a new paragraph.

Everything works, the only problem I have is that I also can drop off anywhere in the entire tree 'frame '. I want that the user must release on

an element in the tree, not anywhere in the treeframe.

My second question is, can I also drag the entire image / rectangle instead of the standard small window box when a user tries to drag something? I read that

in JavaFX 8 would be possible, here you are able to transmit an entire image to the dragboard. My goal was to set the opacity of the rectangle enjoy 20% and then paste

the rectangle with the mouse, and if the user releases the mouse, the rectangle must return to its position of moose with full opacity. It would be a "nice to have", but

the first problem is unimportant.

This example should work out of the box:

package de.hauke.edi;

import java.io.IOException;

import de.cargosoft.edi.eservicemanager.Main3.ModuleTreeItem;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.ImageView;
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.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.Duration;

public class Main4 extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {

        BorderPane border = new BorderPane();
        Scene scene = new Scene(border, 700, 700);

        // setup tree
        TreeView<String> tree = new TreeView<String>();
        TreeItem<String> rootItem = new TreeItem<String>("Root");
        rootItem.setExpanded(true);
        for (int i = 1; i < 6; i++) {
            TreeItem<String> item = new TreeItem<String>("Node" + i);
            rootItem.getChildren().add(item);
        }
        tree.setRoot(rootItem);

        // setup boxes
        final Rectangle red = new Rectangle(100, 100);
        red.setFill(Color.RED);
        final Rectangle green = new Rectangle(100, 100);
        green.setFill(Color.GREEN);
        final Rectangle blue = new Rectangle(100, 100);
        blue.setFill(Color.BLUE);

        addDragFunction(red, "red");
        addDragFunction(green, "green");
        addDragFunction(blue, "blue");

        tree.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {

            public TreeCell<String> call(TreeView<String> arg0) {
                final TreeCell<String> treeCell = new TreeCell<String>() {
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (item != null) {
                            setText(item);
                        }
                    }
                };

                treeCell.setOnDragOver(new EventHandler<DragEvent>() {
                    public void handle(DragEvent event) {
                        
                        event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
                        
                        event.consume();
                    }
                });

                treeCell.setOnDragDropped(new EventHandler<DragEvent>() {
                    public void handle(DragEvent event) {
                        
                        Dragboard db = event.getDragboard();
                        if (db.hasString()) {
                            TreeItem<String> item = new TreeItem<String>("New "+db.getString() + " colored element dropped");
                            treeCell.getTreeItem().getChildren().add(item);
                            treeCell.getTreeItem().setExpanded(true);
                        }
                        event.consume();
                    }
                });

                return treeCell;
            }

        });

        // add elements to stage
        VBox vbox = new VBox();
        vbox.getChildren().add(red);
        vbox.getChildren().add(green);
        vbox.getChildren().add(blue);

        border.setLeft(tree);
        border.setRight(vbox);

        primaryStage.setScene(scene);
        primaryStage.show();

    }

    private void addDragFunction(final Rectangle control, final String code) {
        control.setOnDragDetected(new EventHandler<MouseEvent>() {
            public void handle(MouseEvent event) {
                Dragboard db = control.startDragAndDrop(TransferMode.ANY);

                /* put a string on dragboard */
                ClipboardContent content = new ClipboardContent();
                content.putString(code);
                db.setContent(content);

                event.consume();
            }
        });

        control.setOnDragDone(new EventHandler<DragEvent>() {
            public void handle(DragEvent event) {
                event.consume();
            }
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}
























Thanks for any help!

Everything works, the only problem I have is that I also can drop off anywhere in the entire tree 'frame '. I want that the user must release on

an element in the tree, not anywhere in the treeframe.

Just surround the code in methods of grip for your setOnDragOver and setOnDragDropped event handlers with

If (! treeCell.isEmpty ()) {...}

You could add more sense here if you need, for example to prevent the user from falling on the root or to limit the number of children has fallen on a single node.

For the JavaFX 8 image features with stretching, in your setOnDragDetected Manager, change the opacity, create an image of the control by calling snapshot() and pass the image to the setDragView (...) method of the Dragboard. In the setOnDragDone Manager, resume the opacity.

Tags: Java

Similar Questions

  • Is drag-and - d├⌐poser supported by TreeItem?

    Hello

    I am currently working with a JavaFx-2 TreeView representing a file system.

    I want to activate [drag and drop | http://docs.oracle.com/javafx/2/drag_drop/jfxpub-drag_drop.htm] to move operations, but it looks like TreeItem does not include slide event listeners. I was able to implement drag and drop on the TreeView encompassing, but it does not work for the sub-items.

    Am I missing something, or are drag and drop of events not taken in charge for the treeitem elements yet?

    Related SO question: http://stackoverflow.com/questions/11242847/is-drag-and-drop-supported-by-treeitem

    You must implement drag on drop on the TreeCell.

    Write a CellFactory like this:

    TreeView treeView = new TreeView();
            treeView.setCellFactory(new Callback, TreeCell>() {
                @Override
                public TreeCell call(TreeView stringTreeView) {
                    TreeCell treeCell = new TreeCell() {
                        protected void updateItem(String item, boolean empty) {
                            super.updateItem(item, empty);
                            if (item != null) {
                                setText(item);
                            }
                        }
                    };
    
                    treeCell.setOnDragDetected(new EventHandler() {
                        @Override
                        public void handle(MouseEvent mouseEvent) {
    
                        }
                    });
    
                    return treeCell;
                }
            });
    
  • Assessment of JavaFX 2.0 - treeView example

    Hello.
    I am to evaluate JavaFx 2.0 to replace our framework for the redesign of legacy applications with new capabilities. I'm looking for an example of an implementation of treeView with node, add, delete the node, rename the node, drag and drop nodes in the tree and so on. Does anyone have an example or point to some documents, book, etc..

    The documentation is not very clear for a beginner like me:(donc toute aide sera très utile, j'ai besoin de mettre àle point un prototype bientôt pour justifier une décision.)

    Thanks in advance.
    Paul

    Not everything you ask is explained here, but it would be a good start if you haven't read it already =>
    http://docs.Oracle.com/JavaFX/2.0/ui_controls/tree-view.htm#BABDEADA

    For more information you can also watch the TableView tutorial which, although a different control, works in the same way =>
    http://docs.Oracle.com/JavaFX/2.0/ui_controls/table-view.htm#CJAGAAEE

    Information on doing drag and drop work is here-online
    http://docs.Oracle.com/JavaFX/2.0/drag_drop/jfxpub-drag_drop.htm

    There is a very simple TreeView throughout, example of application =>
    http://download.Oracle.com/otndocs/products/JavaFX/2.0.2/samples/ensemble/index.html#samples/controls/tree/tree%20View

  • You can drag down to default widgets?

    I just bought a new iPhone with iOS 10 7. On my 5s, I could swipe down from the top of the screen and automatically bring up the widgets where I had time at the top. It was perfect because no matter what I did everything I had to do was shot down and the weather seems. Now when I slip down to the Notifications by default, I rarely, if ever actually used. I then have to drag to the right to open the widgets. Is it possible to ensure that you can drag down to jump to widgets instead of having a blank page of Notifications and then drag to the right? It now takes twice as long to perform the same simple task.

    Hey BKavett,

    I understand that you liked using the view today in iOS 9. The good news is that this feature is still available, but has changed the way to access it. In order to access the view today in iOS 10, slide between the left edge of the home screen or the lock screen. Take a look at this link for more information:

    View today - iPhone

    Thank you for coming to the communities of Apple Support and let us know if we can be of further assistance.

  • Drag the photos in a book stops working

    Hello

    I'm creating a photo book in pictures but having now with the software starts don't afford to drag and drop pictures in the bar downstairs in the frames in images in the book. Is going to happen with images (so choose a different does not matter) and apply to all frames. Evolution book I am does not solve it no more.

    Photos reboot usually solves the problem, but often not for long. It is to make the process of making my book painful and difficult. Restart the Mac has also not resolved.

    I'm a little scared that my search for solutions digs up iPhoto do the same thing to people in 2006!

    I use Sierra 10.12 on an iMac 27 inches of 2012.

    Any suggestion is appreciated!

    Stephen

    We can't see you so derails are essential - why you can't drag photos in frames? What is the error you get or what exaclty does haopen?

    LN

  • Why can't just drag photos on my desktop with Sierra 10.12 and Photos 2.0?

    I used to be able to simply drag a photo from Photos or iPhoto directly on the desktop of my iMac.  Then, I was able to drag the photos located on my desktop to eBay, Craigslist or other sites that I use to sell items.  Now, it seems I have to use a visit to first export the photos to the office, open finder, click Desktop and THEN dragging them to my desktop iMac computer.  Am I missing something or is this the new method to move photos on my iMac desktop.  I apologize for what may be a very stupid question, but I can't help but feel that I lost the ability to make effective use of the Photos and my iMac.  For any help or suggestion is greatl

    By dragging from the photo browser to the computer desktop still works for me.  As soon as I drag to an empty space on the desktop the pointer turns into a green '+' and I can put the photo.

    If it does not work for you, there could be something problematic about the photos you want to drag.  Photos can be edited or exported? What is the file type of the photos you are trying to drag-and - drop?

    Using iCloud photo library? You have iCloud allowed to sync your desktop and Documents folder with iCloud?

  • IOS10 - cannot get the control center by dragging to the top.  Nothing happens in any screen by sliding upwards

    I have an iphone 6. I've updated to ios10.  Everything seems to work fine except that I am unable to drag upward to get the control center.  What can I do to make this work?

    Hello

    The following steps may help:

    • Go to settings > Control Center:
      • Check that the options on the lock screen and access in applications are turned on.
  • Anyone know why my iTunes songs don't drag and drop MP3?

    I just bought a Sandisk Clip Jam MP3 player.  (Sorry, Apple: my new Shuffle was too difficult to use without screen.)  I tried to drag and drop the music files I want on that to his library iTunes x 12 on my Macbook, but no sign appears when I do that, and they don't "stick".  I followed the instructions in the manual. Anyone have this experience with iTunes? Must be a good reason, but I don't know what it is. Thank you!

    If he plays that mp3s are the tracks that you drag in mp3 format?

  • How to restore the calculator application to drag it to the top of the screen to control

    Can how I restore the calculator to drag it to the top of the screen to control center? It seems to have been removed from the bottom row? Thank you

    Are - what you got to remove the calculator of your applications on the phone? You can find the calculator on the phone itself? If you find it, then try a reset. Hold down the home and sleep/wake together until you see the Apple logo and then release. The phone will restart. There are no settings to change what appears on the display. See if the reset solves your problem.

    EDIT: what exactly you see in the Control Center? If you see only controls the music, then drag to the right to see the rest of the access to the control center.

  • I broke my screen, and now cannot drag to open it.  I have an alarm that don't go.  I also want to synchronize my phone before I take it buy a new one.  Is it possible to synchronize and watch my calendar for the next few days?

    I broke my screen, and now cannot drag to open it.  I have an alarm that don't go. I can let the battery die I know but I really want to be able to access and examine what are the texts I received today. I think that my ICloud is can be disabled. I also want to synchronize my phone before I take it buy a new one.  Is it possible to synchronize and watch my calendar for the next few days?

    Without entering your access code, it is not possible, unless your time is in iCloud. If this is the case, connect to iCloud.com to check your calendar.

    Good luck.

  • When I drag imessage photos in iphoto, it shows that they give, but then I can't find photos in iphoto. and when I open the finder there is no option of 'media' in the side menu.

    Could not find photos in iphoto after drag / drop imessage

    Correct, there is no media browser in the finder - only for download or open or install windows

    They show in the last import? check your type of event - they are probably n = just not where you are looking - in the last import done right click on one and show events - an event should open with your photo display - if not arrive then back up your iPhoto library and hold down the option and command keys at the launch of the iPhoto and repair permissions hen rebuild the database

    LN

  • How to drag the folder main back in the sidebar of the window

    Hello

    By mistake, I dragged the main folder (root?) of the sidebar of the window. I was trying to drag in the Prefs > projector > protection of personal information in order to reindex Spotlight. When I let go, she went from Ottoman.

    I am Elmer, I think it was called "Elmer Macbook" or something like that. No idea how it go back in the sidebar of the window?

    The best

    Elmer

    Hello

    Open the Finder, then go to the Finder menu bar and click Edit > undo move

  • RuPaul Drag Race 2 all stars is not streaming.

    So the third episode of the so-called Drag Race All Stars 2 became available to 02:00, but I get an error trying to listen in on AppleTV, download on the iMac, nothing... Not only this, the whole of the season does not! What is happening on iTunes?

    I have the same problem with the same show, but also fear of The Walking Dead.

  • How to drag and drop photos to photos on memory stick

    seems I can is no longer just drag and drop photos on memory stick of my mac progesterone... .any suggestions?

    I don't know your problem makes the impression that I do not understand the relevance of female hormones.

  • Cannot drag AAC ringtones to the iPhone 6

    So there's iTunes decided to swap my record library iTunes for something that he created, so I lost most of my music (later everything came with the boring exclamation point next to it, feel stupid iTunes cannot locate the files - redirect tried it back but that did not work). SO, since I am free three hours today (little I didn't know it would take approximately eight hours), I went ahead and resynced to create a new library.

    Lost most of the music on my iPhone but I can re-import ALL my CDs later, I guess. What I have in my phone REALLY are custom ringtones. I currently have around thirty of them as *.mp3s and am familiar with the creation of versions AAFC using iTunes - BUT now every time I have create AAC version and try to drag a lining to my camera, box appears around the entire device on the left side window, shakes and shimmies for one second and will not let me transfer the ringtone.

    The phone held all these rings before this last re-sync, so I know it's possible. Does anyone know why I can't drag these now?

    Thanks for any help. Sorry to say, but I hate iTunes...

    Tom

    Hey Mr. Tom Foolery.

    Sorry about your headache with the music. I took to the look and think this may help you with AAC files is transformed into ringtones iTunes and your phone can recognize. Let me know if it helps.

    http://www.WikiHow.com/make-ringtones-for-the-iPhone

Maybe you are looking for

  • iOS10 avoiding using fingerprinting on the Lockscreen

    Not wanting to seem too "sheet of Tin-hat", but I have not being updated to iOS10 again and can not - if I am forced to give Apple my fingerprints. I've never used the footprint unlock the thing on my iPhone before, because I always felt that it was

  • Apps is stuck

    iPhone & iPad: apps got stuck "Installation", "Waiting" or "loading". This application developed using flex builder SDK Air 4.14.1. Latest version of ios is 9.2. Is there a solution?

  • Error 2203 when try to install Microsoft SQL Native Client

    Hello, good afternoon. I tried so many ways to install the program, change the security of the folder, etc, but for a reason, the error still occurs. I don't know if I just forgot to do something else; I use Windows Server 2012 Standard. http://answe

  • Update issues that are necessary to I can be online with my router__

    I keep trying to get updates, but I get the message to try again.  Also, I and a "WindowsUpdate_800B0100" "WindowsUpdate_dt000"

  • Menu icons are missing T4i

    My daughter used my canon rebel t4i and now some of my menu icons are missing.  She changed a certain setting and I wanted to erase all settings, well, this is not yet an option Please HELP!