views:

36

answers:

1

Hello, everyone!

Please point me to some tutorials or other explaining examples about how to register and use modification listeners with Java's DOM implementation.

On the web I find only Javascript or Flex examples.

My target is to get to know when a Node was modified.

I tried out several approaches, nothing works. Could it be that Java's DOM doesn't support this feature?

+1  A: 

Got it!

Casting was the trick!

I was looking for implementations of org.w3.dom.events.EventTarget, but it seems that only internal classes implement it. So it have just to be casted by hand (by just assuming that Node instanceof EventTarget).

org.w3c.dom.events.EventListener myModificationListener =
  new org.w3c.dom.events.EventListener() {

    @Override
    public void handleEvent(Event e) {
      if (e instanceof MutationEvent) {
        MutationEvent me = (MutationEvent) e;
        System.out.println("type: " + me.getType()
          + ", dest: " + me.getTarget());
      }
    }

  };

Node someDomNode = ...

// here the unusual casting magic happens
((EventTarget) node).addEventListener(
  "DOMSubtreeModified", // constant
  myModificationListener, true);

// modify the node here by appending a child
// -> listener gets invoked
java.is.for.desktop