Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JTree Deleting painting problem

Posted on 2005-04-28
16
Medium Priority
?
337 Views
Last Modified: 2007-12-19
Hi!

I'm using a JTree component, and when I remove all nodes or replace them by using TreeModel, texts painted on JTree and used by nodes continues showing on JTree, is like remove them need to be repainted or another thing. I Tried .Repaint() but nothing happen, Thanks for your help on solving this problem!
0
Comment
Question by:accarvajal
  • 8
  • 7
16 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13889974
you need your tree model to fire the appropriate event to inform listeners of what has been removed.
0
 

Author Comment

by:accarvajal
ID: 13890016
All nodes are well removed, the only problem is continues showing just text and images on screen, but internally nodes no longer exists. So, still is necessary to fire event?
0
 
LVL 92

Expert Comment

by:objects
ID: 13890177
yes, whenever you change the model it needs to fire an event'
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:accarvajal
ID: 13890537
Ah! but I use a class Arbol derived from JTree and super constructor calling new DefaultTreeModel(...), since I don't use my own treemodel, So in this case how is it?
0
 
LVL 92

Expert Comment

by:objects
ID: 13890557
doesn't matter whose tree model you use, that tree model needs to fire the event.
how do u remove nodes from model?
0
 

Author Comment

by:accarvajal
ID: 13890671
rootNode.removeAllChildren();
treeModel.reload();

Is there anything wrong here?
0
 
LVL 92

Expert Comment

by:objects
ID: 13890705
instead of reload() i'd call

treeModel.nodeStructureChanged(rootNode);
0
 

Author Comment

by:accarvajal
ID: 13890782
So!, code would be :

rootNode.removeAllChildren();
treeModel.nodeStructureChanged(rootNode);

and I add event methods for listening Remove, Add, and Change, is this so?
0
 
LVL 92

Expert Comment

by:objects
ID: 13890807
nodeStructureChanged() will fire events to the listeners
0
 

Author Comment

by:accarvajal
ID: 13890830
Ok, I'll put this code, and I'll comment you, if it works, thanks
0
 

Author Comment

by:accarvajal
ID: 13891387
No!, it didn't work, I did this:

public class Arbol extends JTree {
      private DefaultTreeCellRenderer m_objTreeRender = null;
      private DefaultMutableTreeNode m_rootNode = null;

    public Arbol(DefaultMutableTreeNode rootNode,
                      boolean bEditable) {
            super(new DefaultTreeModel(rootNode));
            setEditable(bEditable);
            getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
            setShowsRootHandles(true);
            m_objTreeRender = new          DefaultTreeCellRenderer();
            m_rootNode = rootNode;
      }

       public void EliminarTodosLosNodos() {
            setExpandedState(getPathForRow(0), false);
            m_rootNode.removeAllChildren();
getTreeModel().nodeStructureChanged(m_rootNode);
//            getTreeModel().reload();
      }
}

in main class:

TreeModelListener evArbol = new TreeModelListener() {
        public void treeNodesChanged(TreeModelEvent e) {
        }
        public void treeNodesInserted(TreeModelEvent e) {
        }
        public void treeNodesRemoved(TreeModelEvent e) {
        }
        public void treeStructureChanged(TreeModelEvent e) {
        }

        public DefaultTreeModel getTreeModel() {
            return (DefaultTreeModel)getDefaultTreeModel();
      }
};

DefaultMutableTreeNode rootNode = new   DefaultMutableTreeNode("Components");
m_treeComponentes = new Arbol(rootNode, false);
m_treeComponentes.addMouseListener(evMouseArbol);
m_treeComponentes.getTreeModel().addTreeModelListener(evArbol);

When I create these nodes:

- Components
  - Add node
  - Add Item
  - Delete Item/node

JTree shows nodes fine! but then I need to recharge with new nodes like this:

- Components
  - Add Menu
  - Add Menu Item
  - Delete Menu/Item

I call EliminarTodosLosNodos() first and then I create new nodes but not using constructor else createNewNodes method. when it is finished JTree shows me:

- Components
  - Add node
  - Add Item
  - Delete Item/node

What happened about 'Add Menu', 'Add Menu/Item', 'Delete Menu/Item'
0
 

Author Comment

by:accarvajal
ID: 13891393
Sorry!, I made a mistake:

public DefaultTreeModel getTreeModel() {
          return (DefaultTreeModel)getDefaultTreeModel();
}

is into Arbol class not in main class
0
 
LVL 92

Expert Comment

by:objects
ID: 13891751
It would seem far simpler to use a different model instead of updating the existing one.
0
 
LVL 5

Accepted Solution

by:
Naeemg earned 80 total points
ID: 13892370
did u try,
urTree.updateUI();
?
0
 

Author Comment

by:accarvajal
ID: 13894340
objects! excuse me could you please show me a little sample about you telling me.

Naeemq, Let me try your advice!
0
 
LVL 92

Expert Comment

by:objects
ID: 13898506
create a new model and use the tables setModel() method to change the tree
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

810 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