Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JTree question (add/remove)

Posted on 1999-10-17
5
Medium Priority
?
211 Views
Last Modified: 2008-02-26
HI

I have a JTree component and I need to know :
1. how to delete a node from it
2. how to add a node and the new node will be editable when it added.

the foloowing code doesn't work:
to remove:
DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getSelectionPath().getLastPathComponent();
node.getParent().remove(node);

to add:
DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getSelectionPath().getLastPathComponent();
node.add(new DefaultMutableTreeNode("new node"));


thanks.
0
Comment
Question by:shaharh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 

Author Comment

by:shaharh
ID: 2133563
Edited text of question.
0
 
LVL 2

Expert Comment

by:sprinkmeier
ID: 2134274
Did you update the GUI using
DefaultDataModel.notifyXXXXXX()
?
(going from mmeory here, might not be exactly right, but basically changeing the data is not enough, you have to tell the GUI that the data has changed too)
0
 
LVL 5

Accepted Solution

by:
msmolyak earned 200 total points
ID: 2134444
Rather than using the tree class to add and remove nodes you should be using the corresponding methods in the tree model. The tree (its view) will be notified automatically.

See DefaultTreeModel.insertNodeInto() and
DefaultTreeModel.removeNodeFromParent()


0
 

Author Comment

by:shaharh
ID: 2134743
can you send me some code ?
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 2135599
I defined these two methods in the node class to add and remove children. Class RwAbstractTreeNode is the subclass of DefaultMutableTreeNode.

    /**
     * Add a child node to this node.
     */
    public void insertChildNode(RwAbstractTreeNode node)
    {
        DefaultTreeModel treeModel = (DefaultTreeModel) parentTree.getModel();
       
        node.setTree(getTree());

        treeModel.insertNodeInto(node, this, getChildCount());
    }

    /**
     * Remove a child node from this node.
     */
    public void removeChildNode(RwAbstractTreeNode node)
    {
        // Remove the child node from the tree
        DefaultTreeModel treeModel = (DefaultTreeModel) parentTree.getModel();

        treeModel.removeNodeFromParent(node);
    }
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question