Link to home
Start Free TrialLog in
Avatar of accarvajal
accarvajal

asked on

JTree Deleting painting problem

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!
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you need your tree model to fire the appropriate event to inform listeners of what has been removed.
Avatar of accarvajal
accarvajal

ASKER

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?
yes, whenever you change the model it needs to fire an event'
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?
doesn't matter whose tree model you use, that tree model needs to fire the event.
how do u remove nodes from model?
rootNode.removeAllChildren();
treeModel.reload();

Is there anything wrong here?
instead of reload() i'd call

treeModel.nodeStructureChanged(rootNode);
So!, code would be :

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

and I add event methods for listening Remove, Add, and Change, is this so?
nodeStructureChanged() will fire events to the listeners
Ok, I'll put this code, and I'll comment you, if it works, thanks
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'
Sorry!, I made a mistake:

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

is into Arbol class not in main class
It would seem far simpler to use a different model instead of updating the existing one.
ASKER CERTIFIED SOLUTION
Avatar of Naeemg
Naeemg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
objects! excuse me could you please show me a little sample about you telling me.

Naeemq, Let me try your advice!
create a new model and use the tables setModel() method to change the tree