Domanda

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.

È stato utile?

Soluzione

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top