Removing a node in a tree

Im using this line of code
var selectedNode:XML = XML(MyTree.selectedItem);
and i want to remove the selectedNode from the tree... The
only way that I have found to remove the node is to use these lines
of code which seems ridiculous just to remove the node
var parentChildren:XMLList =
XMLList(selectedNode.parent().children());
for (var i:int = 0; i < parentChildren.length(); i++) {
if (parentChildren
.@label == selectedNode.@label) {
delete parentChildren;
The only thing that I have found is to use the XMLNode object
because it has removeNode method... Except I can't use that either
because I get a warning when I try to cast the selectedItem into a
XMLNode.
Does anyone know how to simplify a deletion of a node.

First, XMLNode is an object belonging to the old flash xml
style xml stuff,
and is not part of the new e4x XML class. Neither is
XMLDocument. In the new
XML, yo only have XML and XMLList to work with.
As far as delete, it is a bit of a PITA. I am amazed that we
have to use the
top-level object delete, but that is the case.
Now you should be able to just do:
delete selectedNode;
Try that first..However, looking back at my own code I
didn't do this.
Instead, I use this function:
//deletes the currently selected node
private function deleteSelectedNode():void
var nodeToDelete:XML = XML(treeData.selectedItem);
//reference to the
node to delete
var xlcParent:XMLListCollection = new
XMLListCollection(nodeToDelete.parent().children());
//collection of nodes
containing the node to delete
var iIndex:int = xlcParent.getItemIndex(nodeToDelete);
//index of node to
delete in collection
xlcParent.removeItemAt(iIndex); //remove the node
//treeData.invalidateList(); //refresh the tree
}//deleteSelectedNode
Maybe that technique will work for you.
Tracy

Similar Messages

  • Can't remove a node from a tree

    I am using the custom tree dataDescriptor provided in Flex live
    doc. It works for creating the tree and add notes, however when I
    try to remove a node from the tree it cant work. Does anyone have
    any idea?
    This is the code for MyCustomeTreeDataDescriptor.as
    package
    import mx.collections.ArrayCollection;
    import mx.collections.CursorBookmark;
    import mx.collections.ICollectionView;
    import mx.collections.IViewCursor;
    import mx.events.CollectionEvent;
    import mx.events.CollectionEventKind;
    import mx.controls.treeClasses.*;
    public class MyCustomTreeDataDescriptor implements
    ITreeDataDescriptor
    // The getChildren method requires the node to be an Object
    // with a children field.
    // If the field contains an ArrayCollection, it returns the
    field
    // Otherwise, it wraps the field in an ArrayCollection.
    public function getChildren(node:Object,
    model:Object=null):ICollectionView
    try
    if (node is Object) {
    if(node.children is ArrayCollection){
    return node.children;
    }else{
    return new ArrayCollection(node.children);
    catch (e:Error) {
    trace("[Descriptor] exception checking for getChildren");
    return null;
    // The isBranch method simply returns true if the node is an
    // Object with a children field.
    // It does not support empty branches, but does support null
    children
    // fields.
    public function isBranch(node:Object,
    model:Object=null):Boolean {
    try {
    if (node is Object) {
    if (node.children != null) {
    return true;
    catch (e:Error) {
    trace("[Descriptor] exception checking for isBranch");
    return false;
    // The hasChildren method Returns true if the node actually
    has children.
    public function hasChildren(node:Object,
    model:Object=null):Boolean {
    if (node == null)
    return false;
    var children:ICollectionView = getChildren(node, model);
    try {
    if (children.length > 0)
    return true;
    catch (e:Error) {
    return false;
    // The getData method simply returns the node as an Object.
    public function getData(node:Object,
    model:Object=null):Object {
    try {
    return node;
    catch (e:Error) {
    return null;
    // The addChildAt method does the following:
    // If the parent parameter is null or undefined, inserts
    // the child parameter as the first child of the model
    parameter.
    // If the parent parameter is an Object and has a children
    field,
    // adds the child parameter to it at the index parameter
    location.
    // It does not add a child to a terminal node if it does not
    have
    // a children field.
    public function addChildAt(parent:Object, child:Object,
    index:int,
    model:Object=null):Boolean {
    var event:CollectionEvent = new
    CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
    event.kind = CollectionEventKind.ADD;
    event.items = [child];
    event.location = index;
    if (!parent) {
    var iterator:IViewCursor = model.createCursor();
    iterator.seek(CursorBookmark.FIRST, index);
    iterator.insert(child);
    else if (parent is Object) {
    if (parent.children != null) {
    if(parent.children is ArrayCollection) {
    parent.children.addItemAt(child, index);
    if (model){
    model.dispatchEvent(event);
    model.itemUpdated(parent);
    return true;
    else {
    parent.children.splice(index, 0, child);
    if (model)
    model.dispatchEvent(event);
    return true;
    return false;
    // The removeChildAt method does the following:
    // If the parent parameter is null or undefined, removes
    // the child at the specified index in the model.
    // If the parent parameter is an Object and has a children
    field,
    // removes the child at the index parameter location in the
    parent.
    public function removeChildAt(parent:Object, child:Object,
    index:int, model:Object=null):Boolean
    var event:CollectionEvent = new
    CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
    event.kind = CollectionEventKind.REMOVE;
    event.items = [child];
    event.location = index;
    //handle top level where there is no parent
    if (!parent)
    var iterator:IViewCursor = model.createCursor();
    iterator.seek(CursorBookmark.FIRST, index);
    iterator.remove();
    if (model)
    model.dispatchEvent(event);
    return true;
    else if (parent is Object)
    if (parent.children != undefined)
    parent.children.splice(index, 1);
    if (model)
    model.dispatchEvent(event);
    return true;
    return false;
    This is my tree definition:
    <mx:Tree width="143" top="0" bottom="0" left="0"
    height="100%"
    id="publicCaseTree"
    dataDescriptor="{new MyCustomTreeDataDescriptor()}"
    dataProvider="{ac}"
    defaultLeafIcon="@Embed('assets/caseIcon.png')"
    change="publicTreeChanged(event)"
    dragEnabled="true"
    dragMoveEnabled="false"/>
    This is how I remove the selected node from the tree. When
    Delete button is clicked, the doDeleteCase function is
    exectuted.
    public function publicTreeChanged(event:Event):void {
    selectedNode =
    publicCaseTree.dataDescriptor.getData(Tree(event.target).selectedItem,
    ac);
    public function doDeleteCase(event:Event):void{
    publicCaseTree.dataDescriptor.removeChildAt(publicCaseTree.firstVisibleItem,
    selectedNode, 0, ac);
    Any help would be appreciated.Thanks.

    Finally I removed nodes from tree, but not sure I did in the
    right way. Anybody encounter the same problem, please
    discuss.

  • Remove root node from tree ...

    hi all,
    how can i delete the root node from a tree ???
    i've tried:
    tree.removeAll();
    this removes all nodes from the tree except the root.
    thanks.

    try this:
    tree.setRootVisible(false);
    Bye!

  • 3750x Stack UTIL-3-TREE: Data structure error--attempt to remove an unthreaded node from a tree

    A Cisco Stack 3750X switch report the following error message:
    %UTIL-3-TREE: Data structure error--attempt to remove an unthreaded node from a tree.
    every minute +-20 sec
    Cisco IOS Software, C3750E Software (C3750E-IPBASEK9-M), Version 15.0(2)SE4, RELEASE SOFTWARE (fc1)
    analog bug: https://tools.cisco.com/bugsearch/bug/CSCsz93221

    WS-C3750G-24PS-E C3750-IPBASEK9-M version 12.2(53)SE2
    After implementing 802.1x with Avaya IP phones
    %UTIL-3-TREE: Data structure error--attempt to remove an unthreaded node from a tree
    Port then fails authentication and goes into vl-err-dis

  • Question on removing a child node in a tree component - JSF

    When I m trying to delete a child node in a tree component the focus is not transfering to the parent node. I m using the setselected method to set the focus to the current node but not sure how to transfer the focus to parent node once I delete a child node. Please advice.

    Doubleposted: [http://forums.sun.com/thread.jspa?threadID=5389876]. Please stick to one topic or use the edit button.

  • Using depth first traversal to add a new node to a tree with labels

    Hello,
    I'm currently trying to work my way through Java and need some advice on using and traversing trees. I've written a basic JTree program, which allows the user to add and delete nodes. Each new node is labelled in a sequential order and not dependent upon where they are added to the tree.
    Basically, what is the best way to add and delete these new nodes with labels that reflect their position in the tree in a depth-first traversal?
    ie: the new node's label will correctly reflect its position in the tree and the other labels will change to reflect this addition of a new node.
    I've searched Google and can't seem to find any appropriate examples for this case.
    My current code is as follows,
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.tree.*;
    public class BasicTreeAddDelete extends JFrame implements ActionListener
        private JTree tree;
        private DefaultTreeModel treeModel;
        private JButton addButton;
        private JButton deleteButton;
        private int newNodeSuffix = 1;
        public BasicTreeAddDelete() 
            setTitle("Basic Tree with Add and Delete Buttons");
            DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root");
            treeModel = new DefaultTreeModel(rootNode);
            tree = new JTree(treeModel);
            JScrollPane scrollPane = new JScrollPane(tree);
            getContentPane().add(scrollPane, BorderLayout.CENTER);
            JPanel panel = new JPanel();
            addButton = new JButton("Add Node");
            addButton.addActionListener(this);
            panel.add(addButton);
            getContentPane().add(panel, BorderLayout.SOUTH);
            deleteButton = new JButton("Delete Node");
            deleteButton.addActionListener(this);
            panel.add(deleteButton);
            getContentPane().add(panel, BorderLayout.SOUTH);    
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400, 300);
            setVisible(true);
        public void actionPerformed(ActionEvent event) 
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
            if(event.getSource().equals(addButton))
                if (selectedNode != null)
                    // add the new node as a child of a selected node at the end
                    DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New Node" + newNodeSuffix++);
                      treeModel.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());
                      //make the node visible by scrolling to it
                    TreeNode[] totalNodes = treeModel.getPathToRoot(newNode);
                    TreePath path = new TreePath(totalNodes);
                    tree.scrollPathToVisible(path);               
            else if(event.getSource().equals(deleteButton))
                //remove the selected node, except the parent node
                removeSelectedNode();           
        public void removeSelectedNode()
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
            if (selectedNode != null)
                //get the parent of the selected node
                MutableTreeNode parent = (MutableTreeNode)(selectedNode.getParent());
                // if the parent is not null
                if (parent != null)
                    //remove the node from the parent
                    treeModel.removeNodeFromParent(selectedNode);
        public static void main(String[] arg) 
            BasicTreeAddDelete basicTree = new BasicTreeAddDelete();
    }      Thank you for any help.

    > Has anybody got any advice, help or know of any
    examples for this sort of problem.
    Thank you.
    Check this site: http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JTree.html

  • How to Remove a Node from JTree?

    I want to remove a node from a JTree but the node may or maynot be visible.
    My method takes a String which is the name of the node.
    The nodes im using are DefaultMutableTreeNode
    This is my code so far but it doesnt work.
    public void removePerson(String pName)
              TreePath node = tree.getNextMatch(pName,0,Position.Bias.Forward);
              tree.expandPath(node);
              tree.removeSelectionPath(node);
    }Any Suggestions or ways which i could achive this?
    Thank you,

    I don't think removeSelectionPath is what you want to use.
    These should help:
    [http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/tree/DefaultTreeModel.html#removeNodeFromParent(javax.swing.tree.MutableTreeNode)|http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/tree/DefaultTreeModel.html#removeNodeFromParent(javax.swing.tree.MutableTreeNode)]
    [http://www.roseindia.net/java/example/java/swing/RemoveNodes.shtml|http://www.roseindia.net/java/example/java/swing/RemoveNodes.shtml]

  • Displaying the entire text of a tree node when the tree isn�t wide enough

    Hi,
    I have a JTree displayed in a JScrollPane, so there is a chance that some of the tree data may be hidden if the tree's width is insufficient, so when the user moves the cursor over a tree node whose text is not completely visible (cut off by the right edge of the scroll pane and/or window), a tooltip is displayed to show the entire node text. So far so good!
    If the user double click a node in the tree a new window is supposed to be opened. This works fine if the tooltip hasn�t been displayed jet, but if it has then the user has to click 3 times to open the window.
    The first time to remove the tooltip and the next 2 opens the window.
    How can I awoid this?
    Thanks!!!!
    :-)Lisa

    Any ideas, please?

  • How to remove the focus from a tree?

    Hello,
    how can I remove the focus from a tree, so that
    none of the elements in the tree is selected?
    I have tried using the following methods:
    setTreeSelection(null);
    setLeadSelection(IWDNode.NO_SELECTION);
    None of them worked.
    Any help highly appreciated.
    Greetings,
    Tomek.

    Hello,
    it seems I have found a (dirty) workaround to this
    problem. If anyone is interested, details below:
    When the user clicks on a tree node (say A tree),
    I do the following:
    1. request focus on the tree selected by the user
       (say B tree):
      IWDTree foldersTree = 
        (IWDTree)thisView.getElement("TreeFolders");
      foldersTree.requestFocus();
    2. invalidate the A tree (which causes the tree
       to disappear)
      wdThis.wdGetContext().nodeInbox().invalidate();
    3. recreate the A tree (filling the context nodes)
    4. unselect the A tree:
    wdContext.nodeInbox().setLeadSelection(IWDNode.NO_SELECTION);
    Thanks a lot to all of you who have responded.
    Greetings,
    Tomek.

  • How can I remove child node from JTree???

    Hi,
    I would like to remove all the child node of my jtree. For instance I would like to remove the c, d, and e nodes. It's possible to remove all the child node or to remove by her name ("c", "d", and "e"). If yes what is the method that it permit to do.
    A-----
    |
    b-------c
    |
    |--------d
    |
    ---------e
    I use the model : DefaultMutableTreeNode
    Thanks

    There are a couple of ways it can be done. If your tree uses DefaultTreeModel as its TreeModel, you can use removeNodeFromParent(). This will remove the node from its parent and effectively remove its children, too. All nodes removed will be garbage-collected if there are no other references to them.
    If your tree model is not the default tree model, but still uses MutableTreeNode, you can use either remove() or removeFromParent() on the node itself, depending on whether you want to remove the node itself or one of its children.
    On the other hand, your tree may use a model that simply "mirrors" another data structure, in which case you would have to remove the node from the other data structure and have it reflected in the model.

  • How to remove all nodes (except root node)from a Jtree?

    How to remove all nodes (except the root node)from a Jtree?

    Either:
    - remove all children of root.
    - save the root node, throws away the tree model, build a new TreeModel with the saved root, set the new TreeModel in the JTree.
    - implement your own TreeModel, which would support an emptyExceptRoot() method.
    IMHO, using the DefautlTreeModel and DefaultMutableTreeNode does lead to all sorts of small problems when the app evolves, and implementing your own TreeNode and TreeModel is not that hard and much more efficient.

  • How to not display nodes in a tree if Oracle roles are NOT used?

    How to not display nodes in a tree if Oracle roles are NOT used?
    We don't use Oracle DB roles to grant users access to Forms from the menu. We use a template and role system of our own. Basically a few tables with templates and roles.
    We want to convert our normal Forms menu to a tree menu and one of our key requirements is that when the tree is populated ONLY nodes with programs (i.e. forms) he has been granted to execute is shown.
    Since we don't use Oracle Roles how to do this in a tree?
    I created a function to show/hide LEAF nodes, BUT problem is that there are sub-menu nodes showing even if the leaf-nodes under it has not being displayed. My function has suppressed it.
    My tree query is like this:
    SELECT
         t.status, LEVEL, t.label, t.icon, t.node VALUE
    FROM
         tma_tree_menu t
    WHERE
    tma_authenticate_sys_chk_role(USER, t.node) = 1
    CONNECT BY
         PRIOR t.node = t.master
    START WITH
         t.MASTER IS NULL
    ORDER SIBLINGS BY
    t.position
    The tma_authenticate_sys_chk_role will return 1 only if the user has access to the form under that node.
    I tried the FTree functions in Forms but even that has nothing.
    Any help would be greatly appreciated.
    Edited by: Channa on Mar 17, 2010 6:49 AM

    Would you share the source code? I guess what I need is how exactly you retreive the user credentials from the DB table and set that boolean variable.
    and then how to condition it in UIX?

  • Runtime error while i add a node in ALV Tree in oops

    i am adding a node to alv tree using oop am passing a work area and when i execute it is going for a dump and it says UC_OBJECTS_NOT_CONVERTIBLE
    and the below where it is bold and italic it is where the dump is occuring
    METHOD ADD_NODE.
    FIELD-SYMBOLS: <TAB1> TYPE standard TABLE,
    <wa> type any.
    assign mt_outtab->* to <tab1>.
    insert line in outtab
    DATA: L_INDEX TYPE SY-TABIX.
    if is_outtab_line is initial.
    create initial line
    data l_dref_wa type ref to data.
    create data l_dref_wa like line of <tab1>.
    assign l_dref_wa->* to <wa>.
    l_index = 0.
    append <wa> to <Tab1>.
    else.
    APPEND IS_OUTTAB_LINE TO <TAB1>. endif.
    L_INDEX = SY-TABIX.
    add node to model
    CALL METHOD ME->ADD_MODEL_NODE
    EXPORTING
    I_RELAT_NODE_KEY = I_RELAT_NODE_KEY
    I_RELATIONSHIP = I_RELATIONSHIP
    IS_NODE_LAYOUT = IS_NODE_LAYOUT
    IT_ITEM_LAYOUT = IT_ITEM_LAYOUT
    I_NODE_TEXT = I_NODE_TEXT
    I_INDEX_OUTTAB = L_INDEX
    IMPORTING
    E_NEW_NODE_KEY = E_NEW_NODE_KEY.
    ENDMETHOD.

    HI Mohsin,
    please refer to the below ....
    might be helpful for u .....
    https://scn.sap.com/thread/2050188
    http://scn.sap.com/message/6407195
    http://r0005001.benxbrain.com/de%28bD1lbiZjPTAwMQ==%29/index.do?onInputProcessing=brai_thread&001_thread_id=1759814%20&001_temp=R3TR|PROG|RCSBI010||P01|
    Hope thiw will help ....
    Regards,
    AKS

  • Fetch the index of the node in a tree

    hi,
    i am using a recursive tree structure,
    i need to get the index value of a selected node in a tree structure,
    can any one help me?
    thanks in advance,
    Regards,
    Meyyappan

    Hello Ganesh,
    in that case, just call
      lr_node_tmp = context_element->get_node().
      lr_parent_element = lr_node_tmp->get_parent_element().
      lr_parent_node = lr_parent_element->get_node().
    Best regards,
    Thomas

  • How to remove a node from nlb at runtime?

    hello,
    i need to temporally exclude a node from an nlb.
    May happen that a server is up and working but the web application i'm balancing is out of sinch with the same application in the others nodes.
    Eg. some static variables are not the same of the same static variables of other nodes, because of a timeout, a write error and so on but the server is still working.
    in this case i need to stop the server from nlb because the information in the web application is not in sinch with other nodes.
    I need to prevent users from being serverd from this out to date server, untill it will became updated, but i need to do this programmatically
    how can i do it?

    "DuoMi" <[email protected]> wrote in message
    news:gnojbf$2sg$[email protected]..
    > How to remove a node from XMLList
    >
    > I want remove the first node from XMLList
    >
    >
    > and how to get the combobox all values string:
    >
    > <data>
    > <value>2</value>
    > </date>
    > <data>
    > <value>5</value>
    > </date>
    > <data>
    > <value>8</value>
    > </date>
    >
    > I need a string whit value "2,5,8"
    >
    > thkan you.~
    I think something like
    comboBox.dataProvider..value.toString()
    will work.

Maybe you are looking for

  • For all entries in read statement

    hi is there any command equivalent to for all entries in read statement

  • "java.io.IOException: Too many open files"  in LinuX

    Hi Developers, * I am continiously running and processing more than 2000 XML files by using SAX and DOM..... * My process is as follows, - Converting the XML file as Document object by DOM.... - And that DOM will be used while creating log file repor

  • RosettaNet DTD

    I saw the messages about reading a node in a RossetaNet xml doc. Question I have is is the DTD needed to read the node using XPath? I suppose what I am reaching for here is what purpose does a DTD serve other than to validate the document? Tks Rob

  • 5.0.2 some continuing issues

    Greetings, I have the most current updates installed in Master Collection. Premiere Pro is a fresh install after previous update issues trashed my install. So it's clean OS as well. 1-I calculated renders, and when I closed the project and came back:

  • RecipeFox doesn't work because of Java issues when I have Java installed

    Recipe Fox grabit has been working but now I keep getting message that I need to install Java when it's already installed. This appears to be after a Firefox update.