1

I got such a trouble. I have an overlay database structure and use JTree to display the items. Here's my simplified view of model:

public class MenuTreeModel implements TreeModel {
private MenuList ml;

public MenuTreeModel( MenuList ml ) {
    this.ml = ml;
}

@Override
public void addTreeModelListener(TreeModelListener l) {

}

@Override
public Object getChild(Object parent, int index) {
    if ( parent.getClass().hashCode() == MenuList.class.hashCode() ){
        return ((MenuList) parent).getStation(index);
    }
    if ( parent.getClass().hashCode() == MenuStations.class.hashCode() ){
        return ((MenuStations) parent).get(index);
    }
    return null;
}

@Override
public int getChildCount(Object parent) {
    if ( parent.getClass().hashCode() == MenuList.class.hashCode() ){
        return ((MenuList) parent).getSize();
    }
    if ( parent.getClass().hashCode() == MenuStations.class.hashCode() ){
        return ((MenuStations) parent).getSize();
    }
    return 0;
}

@Override
public int getIndexOfChild(Object parent, Object child) {
    if ( parent.getClass().hashCode() == MenuList.class.hashCode() ){
        return ((MenuList) parent).getIndexOf((MenuStations) child);
    }
    if ( parent.getClass().hashCode() == MenuStations.class.hashCode() ){
        return ((MenuStations) parent).getIndexOf((MenuCats) child);
    }
    return 0;
}

@Override
public Object getRoot() {
    return ml;
}

@Override
public boolean isLeaf(Object node) {
    return node.getClass().hashCode() == MenuCats.class.hashCode();
}

@Override
public void removeTreeModelListener(TreeModelListener l) {
    // TODO Auto-generated method stub

}

@Override
public void valueForPathChanged(TreePath path, Object newValue) {
    // TODO Auto-generated method stub

}

And when I remove something from ml or its inner items, the tree structure does not change. I couldn't find any method like fireTreeModelChanged.

Yurgen
  • 123
  • 1
  • 13

2 Answers2

1

You would only find a method fireTreeModelChanged() if your model extended a class containing such a method. But unlike TableModel, which has an associated AbstractTableModel containing fireXxx() methods, there is no such class for trees. Your best bet is to either use a DefaultTreeModel, or to define your own event firing methods.

Of course, to be able to implement them, you would need to actually add listeners in your addTreeModelListener() method, instead of not doing anything.

Another thing that should be changed in your code is your comparisons of hash codes. It makes no sense doing that. BTW, two different classes could have the same hash code. Just use

parent.getClass().equals(MenuList.class)

or

parent instanceof MenuList
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Actually I have no idea how firing mehods are implemented. And its so difficult to find something out. – Yurgen Nov 25 '12 at 16:35
  • 1
    It's quite simple actually. They create a TreeModelEvent instance with the appropriate information inside, then iterate over all the listeners that have been added to the model, and call one of their `treeNodesXxx()` method (the appropriate one). You can always look at the source code of DefaultTreeModel to see what it does. But I think you should just use a DefaultTreeModel rather than designing your own, especially if you don't understand this stuff yet. – JB Nizet Nov 25 '12 at 16:39
  • >You can always look at the source code of DefaultTreeModel to see what it does" yep it was the key – Yurgen Nov 25 '12 at 17:30
0

Your piece of code is not enough to resolve your issue. But here is a fine example for dynamic Tree.

DynamicTreeDemo from docs.oracle.com

Download classes DynamicTreeDemo.java and DynamicTree.java then run it.

vels4j
  • 11,208
  • 5
  • 38
  • 63