Home >Java >javaTutorial >How to Implement SwingPropertyChangeSupport for Property Change Observation?

How to Implement SwingPropertyChangeSupport for Property Change Observation?

Linda Hamilton
Linda HamiltonOriginal
2024-11-06 09:06:02384browse

How to Implement SwingPropertyChangeSupport for Property Change Observation?

SwingPropertyChangeSupport Implementation

To implement SwingPropertyChangeSupport and observe property changes in your application, you can refer to the following example:

Example:

<code class="java">import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.event.SwingPropertyChangeSupport;

public class MyObservableClass {

    private SwingPropertyChangeSupport propertyChangeSupport = new SwingPropertyChangeSupport(this);
    private String name; // A property we want to observe

    public void setName(String newName) {
        String oldValue = this.name;
        this.name = newName;
        propertyChangeSupport.firePropertyChange("name", oldValue, newName);
    }

    public String getName() {
        return name;
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
}

// Example Usage
MyObservableClass observableClass = new MyObservableClass();
PropertyChangeListener listener = new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("name")) {
            System.out.println("Name changed to: " + evt.getNewValue());
        }
    }
};
observableClass.addPropertyChangeListener(listener);
observableClass.setName("New Name"); // This will trigger the listener</code>

In this example, we have a class (MyObservableClass) with a property (name) that we want to observe. We use SwingPropertyChangeSupport to notify listeners about changes to the name property. When the setName() method is called, it triggers the firePropertyChange() method, which notifies the registered listeners about the property change.

In the usage section, we add a PropertyChangeListener to the observable class and listen for changes to the name property. When the setName() method is called and the property value changes, the listener is notified and the propertyChange() method is invoked, where we can perform custom actions in response to the property change.

The above is the detailed content of How to Implement SwingPropertyChangeSupport for Property Change Observation?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn