class NodeImageObserver implements ImageObserver {
JTree tree;
DefaultTreeModel model;
Vector nodes = new Vector();
public NodeImageObserver(JTree tree) {
super();
this.tree = tree;
this.model = (DefaultTreeModel) tree.getModel();
}
public void add(DefaultMutableTreeNode node) {
for (int i = 0; i < nodes.size(); ++i) {
DefaultMutableTreeNode temp = (DefaultMutableTreeNode)nodes.get(i);
if (temp.equals(node))
return;
}
nodes.add(node);
}
NodeImageObserver(JTree tree, Vector nodes) {
this.tree = tree;
this.model = (DefaultTreeModel) tree.getModel();
this.nodes = nodes;
}
public boolean imageUpdate(Image img, int flags, int x, int y, int w,
int h) {
if ((flags & (FRAMEBITS | ALLBITS)) != 0) {
for(int i = 0; i < nodes.size(); ++i) {
TreeNode node = (TreeNode) nodes.get(i);
TreePath path = new TreePath(model.getPathToRoot(node));
java.awt.Rectangle rect = tree.getPathBounds(path);
if (rect != null) {
tree.repaint(rect);
}
}
}
return (flags & (ALLBITS | ABORT)) == 0;
}
}
class MyTree extends JTree {
public DefaultTreeModel model;
public MyTree() {
super();
setCellRenderer(new IconRenderer());
model = new DefaultTreeModel(null);
this.setModel(model);
}
class IconRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded,
leaf, row, hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
ImageIcon gificon = new ImageIcon("node.gif");
NodeImageObserver Observer = new NodeImageObserver(tree);
Observer.add(node);
gificon.setImageObserver(Observer);
setIcon(gificon);
return this;
}
}
}
JTree tree;
DefaultTreeModel model;
Vector nodes = new Vector();
public NodeImageObserver(JTree tree) {
super();
this.tree = tree;
this.model = (DefaultTreeModel) tree.getModel();
}
public void add(DefaultMutableTreeNode node) {
for (int i = 0; i < nodes.size(); ++i) {
DefaultMutableTreeNode temp = (DefaultMutableTreeNode)nodes.get(i);
if (temp.equals(node))
return;
}
nodes.add(node);
}
NodeImageObserver(JTree tree, Vector nodes) {
this.tree = tree;
this.model = (DefaultTreeModel) tree.getModel();
this.nodes = nodes;
}
public boolean imageUpdate(Image img, int flags, int x, int y, int w,
int h) {
if ((flags & (FRAMEBITS | ALLBITS)) != 0) {
for(int i = 0; i < nodes.size(); ++i) {
TreeNode node = (TreeNode) nodes.get(i);
TreePath path = new TreePath(model.getPathToRoot(node));
java.awt.Rectangle rect = tree.getPathBounds(path);
if (rect != null) {
tree.repaint(rect);
}
}
}
return (flags & (ALLBITS | ABORT)) == 0;
}
}
class MyTree extends JTree {
public DefaultTreeModel model;
public MyTree() {
super();
setCellRenderer(new IconRenderer());
model = new DefaultTreeModel(null);
this.setModel(model);
}
class IconRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded,
leaf, row, hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
ImageIcon gificon = new ImageIcon("node.gif");
NodeImageObserver Observer = new NodeImageObserver(tree);
Observer.add(node);
gificon.setImageObserver(Observer);
setIcon(gificon);
return this;
}
}
}