Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swing: event listener support class

Is there any preexisting class that helps support add/remove EventListener operations? (kind of like PropertyChangeSupport)

I'm trying to partition my code into a model and view in Java. I have some data that arrives erratically, and would like the model to support some kind of EventListener so that a view can subscribe to changes in the model. The data is numerous + complicated enough that I don't want to have to do the whole fine-grained Javabeans property change support; rather I would just like to allow notification that the model has changed in a coarse way.

how can I best do this?

like image 574
Jason S Avatar asked Apr 28 '10 18:04

Jason S


People also ask

What is the use of Eventlisteners class in Java?

An event listener in Java is designed to process some kind of event — it "listens" for an event, such as a user's mouse click or a key press, and then it responds accordingly. An event listener must be connected to an event object that defines the event.

What are the 3 types of Java Swing containers?

As we mentioned before, Swing provides three generally useful top-level container classes: JFrame , JDialog , and JApplet .

What are the five Java Swing components?

In Java Swing, there are a number of components like a scroll bar, button, text field, text area, checkbox, radio button, etc. All these components together, form a GUI that offers a rich set of functionalities and also allows high-level customization.

Which of the following listener S is are supported by button Swing component?

Listeners that All Swing Components Support Because all Swing components descend from the AWT Component class, you can register the following listeners on any Swing component: component listener. Listens for changes in the component's size, position, or visibility. focus listener.


2 Answers

I would handle that with a ChangeEvent. It's just an indication that something has changed.

As for implementing the add/remove/fire functionality. There is no mechanism like PropertyChangeSupport, but the code is simple enough there's not really a need for it.

private final EventListenerList listenerList = new EventListenerList();
private final ChangeEvent stateChangeEvent = new ChangeEvent(this);

public void addChangeListener(ChangeListener l) {
    listenerList.add(ChangeListener.class, l);
}
public void removeChangeListener(ChangeListener l) {
    listenerList.remove(ChangeListener.class, l);
}
protected void fireChange() {
    for (ChangeListener l: listenerList.getListeners(ChangeListener.class)) {
        l.stateChanged(stateChangeEvent);
    }
}

Note: JComponent provides a protected listenerList object for use by sub-classes.

like image 79
Devon_C_Miller Avatar answered Sep 17 '22 16:09

Devon_C_Miller


I'm not sure if that answers your question, but you could use a javax.swing.event.EventListenerList, it supports add() and remove() operations for your listeners. Then you can iterate over a particular listener subclass to fire events:

for (MyListener listener : listenerList.getListeners(MyListener.class) {
   listener.fireEvent(...);
}
like image 31
Guillaume Avatar answered Sep 20 '22 16:09

Guillaume