Home > Net >  How to listen for property change of a Java static class
How to listen for property change of a Java static class

Time:01-16

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:

// ... 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?

CodePudding user response:

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);
    }
}
  • Related