Question

Je fais une interface graphique qui conjugue espagnol Verbs. Il utilise un TreeMap comme la principale structure de données, qui est rempli de instanciations de la classe Verbe. Chaque instanciation comprend une chaîne qui contient l'infinitif du verbe, comme « hablar » ou « escribir. » Il y a une fonction dans l'interface graphique qui permet à l'utilisateur d'entrer un nouveau verbe en tapant dans son infinitif et sa traduction en anglais.

Je veux savoir comment créer un écouteur qui exécutera un code à chaque fois qu'un nouveau verbe est ajouté - ou retiré - du TreeMap. Comment dois-je prendre?

Était-ce utile?

La solution

You can subclass the TreeMap class, eg.

public class MyTreeMap<K, V> extends TreeMap<K, V> {
    @Override
    public V put(K key, V val) {
        V ret = super.put(key, val);
        myAddCallback(key, val);
        return ret;
    }

    @Override
    public V remove(K key) {
        V ret = super.remove(key);
        myRemoveCallback(key);
        return ret;
    }
}

Another (significantly more involved) option would be to check out AspectJ, an AOP-based JRE.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top