B4J Question TreeItem dragging?

Discussion in 'B4J Questions' started by udg, May 23, 2015.

  1. udg

    udg Expert Licensed User

    Hi all,

    today I tried my first UI app with the wonderful B4J.

    A few minutes on the JavaFX SB 2.0 and I had ready a test layout.
    Then just a couple of lines of code to test it and... surprise, I hit a wall!

    Is there a way to drag&drop TreeItems around so to change the tree structure at will?

    Where can I found some B4J doc about the JFX elements that it directly recognizes (e.g. their events)?

    My ultimate goal is to have a TreeView where :
    - right-clicking on a TreeItem I could show a contex menu to operate on it (e.g removing from the tree, adding a child, changing its color..)
    - a TreeItem could be dragged and dropped on a new TreeView position

    Is there anyhing like that already available? Thanks.

  2. udg

    udg Expert Licensed User

    Ok, combining TreeView's ContexMenu and the SelectedItemChanged event along with a reserved variable of type TreeItem used to hold a copy of currently selected TreeView's selcted TreeItem, I managed to apply an action to a specific TreeItem.

    Still not solved the drag&drop wish.

  3. LucaMs

    LucaMs Expert Licensed User

  4. udg

    udg Expert Licensed User

    I searched the Internet for a ready-made solution to the drag&drop feature but to no avail.
    What follows is a short list of references to code snippets or istructions useful for java programmers (which I am not).
    BTW, I can't see any reference to a TreeCell object in B4J, so at the moment I can't see how to solve the problem.

    This is a possible solution
    And here it's another one
    Oracle suggests something based on Mouse Events and I even found someone's better explaining it along with a reference to Oracle's examples.

    If anyone here added the d&d functionality to the TreeView object, please let me know.

    Anyway, thank you for your attention.

  5. Daestrum

    Daestrum Well-Known Member Licensed User

    Read your links , had a play, came up with this.
    Not elegant but hey, it works (for string items).

    Attached Files:

    LucaMs likes this.
  6. LucaMs

    LucaMs Expert Licensed User

  7. Daestrum

    Daestrum Well-Known Member Licensed User

    Changed it to a library so you don't need all that ugly code in your program.
    Just include the library TreeViewDnD (Note!! It only handles String items in the treeview)
    Dim DnD as JTreeDnD
    '  where tv is the treeview
    Just going to put in library section.
    udg, inakigarm and LucaMs like this.
  8. udg

    udg Expert Licensed User

    Hi Daestrum,

    thank you very much. I owe you a beer!
    I'm going to try your lib on next weekend and report back here on my findings, although I am strongly confident it will properly work as expected.

    Thank you again.

  9. udg

    udg Expert Licensed User

    Hi Daestrum,

    as promised, today I tested your nice library and it worked as expected.. thank you!
    I don't know whether you will ever have the time to let it grow, but just in case ..
    - when dropping it would be nice to set the relative position of a dropped item between its "brothers" (now it goes appended as last item only)
    - eventually dropping "outside" the treeview could start a deleting item event (here, I don't know if that's what an user would expect to happen)
    - upgrading the lib as to let any Object (with a Text string field) to be a TreeItem

    Anyway, thank you again fr your work.
  10. Daestrum

    Daestrum Well-Known Member Licensed User

    1, The dropping between items is difficult, as you basically have no target to drop on and just a few pixel between items.
    I will look if it's possible to, for example, press control on the release to denote add after this item as opposed to making it a child.
    2, Sort of goes against what Drag and Drop is for, if you dragged outside the treeview, you may be wanting to drag the item into another treeview or textbox.
    I'd be a bit pissed if it deleted the item instead. ;)
    3, This should be pretty straightforward to do, I can just make the items <Objects> in place of <String>. (I hope anyways lol - after trying not as easy as I thought having to rewrite almost all routines for treeview/treeitem & cell)
    Last edited: Jun 1, 2015
  11. TomDuncan

    TomDuncan Active Member Licensed User

    Hi Guys,
    Can this be extended to drag from a listview into the treeview?