Home > Java > javaTutorial > body text

How to Implement SwingPropertyChangeSupport for Property Change Observation?

Linda Hamilton
Release: 2024-11-06 09:06:02
Original
307 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!