Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to listen for property change of a Java static class

i’m building a Swing GUI application and i’m trying to change other parts of the GUI when a property in a static class is changed.

This is the class that holds the properties:

package dev.vitto.cna;

import java.beans.*;

public class Project {

    private static PropertyChangeSupport mPcs =
            new PropertyChangeSupport(Project.class);

    private static String projectName = "Progetto senza titolo";

    private static int activeInstrument = 0;

    // ************************ //

    public static void
    addPropertyChangeListener(PropertyChangeListener listener) {
        mPcs.addPropertyChangeListener(listener);
    }

    public static void
    removePropertyChangeListener(PropertyChangeListener listener) {
        mPcs.removePropertyChangeListener(listener);
    }

    // ************************ //

    public static String getProjectName() {
        return projectName;
    }

    public static void setProjectName(String projectName) {
        if (projectName.length() < 1) {
            projectName = "Progetto senza titolo";
        }
        mPcs.firePropertyChange("projectName",
                Project.projectName, projectName);
        Project.projectName = projectName;
    }

    public static int getActiveInstrument() {
        return activeInstrument;
    }

    public static void setActiveInstrument(int activeInstrument) {
        mPcs.firePropertyChange("activeInstrument",
                Project.projectName, projectName);
        Project.activeInstrument = activeInstrument;
    }

}

When i call the setProjectName() method from another part of the program i expect it to activate all the other listeners registered like this, for example, in a class that represents a window:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// ... code

Project.addPropertyChangeListener((PropertyChangeEvent event) -> {
     if("projectName".equals(event.getPropertyName())){
         label.setText(event.getNewValue());
     }
});

//... code

Unfortunately, that doesn’t happen. I have researched a lot and this seems the only correct way to do it. Any idea on how to make it work?

>Solution :

The solution is to make your class and code stateful and OOP-compliant, meaning that most all of the fields and methods in the code that you’ve posted should be instance fields and methods, with exceptions for constants, utility methods, and the main method.

Side note: you should set your property before calling the notify method, so that the property has been properly set when listeners are notified. Also best to use constants for property names so that there can be no chance of typographical errors slipping in.

And so for example:

import java.beans.*;

public class Project {
    public static final String ACTIVE_INSTRUMENT = "active instrument";
    public static final String PROJECT_NAME = "project name";
    public static final String DEFAULT_PROJECT_NAME = "Progetto senza titolo";
    private PropertyChangeSupport mPcs = new PropertyChangeSupport(this);
    private String projectName = DEFAULT_PROJECT_NAME;
    private int activeInstrument = 0;

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

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

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        if (projectName == null || projectName.length() < 0) {
            return;
        }
        String oldValue = this.projectName;
        String newValue = projectName;
        this.projectName = projectName;
        mPcs.firePropertyChange(PROJECT_NAME, oldValue, newValue);
    }

    public int getActiveInstrument() {
        return activeInstrument;
    }

    public void setActiveInstrument(int activeInstrument) {
        int oldValue = this.activeInstrument;
        int newValue = activeInstrument;
        this.activeInstrument = activeInstrument;
        mPcs.firePropertyChange(ACTIVE_INSTRUMENT, oldValue, newValue);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading