简体   繁体   English

如何监听a的属性变化 Java static class

[英]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.我正在构建一个 Swing GUI 应用程序,当 static class 中的属性更改时,我试图更改 GUI 的其他部分。

This is the class that holds the properties:这是拥有属性的 class:

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:当我从程序的另一部分调用setProjectName()方法时,我希望它能激活像这样注册的所有其他侦听器,例如,在代表 window 的 class 中:

// ... 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?关于如何使其工作的任何想法?

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.解决方案是使您的 class 和代码有状态且符合 OOP,这意味着您发布的代码中的大多数字段和方法都应该是实例字段和方法,常量、实用程序方法和主要方法除外方法。

Side note: you should set your property before calling the notify method, so that the property has been properly set when listeners are notified.旁注:您应该在调用 notify 方法之前设置您的属性,以便在通知侦听器时正确设置该属性。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM