简体   繁体   中英

Is this reasoning about PropertyChangeListener correct?

I have some doubts about the use of the PropertyChangeListener interface.

I have a class named GUI that implements the PropertyChangeListener interface.

In this class I have the following method that create and show a JFrame ( LoginFrame is a custom class that extends JFrame ):

private void showLoginFrame() {
    loginFrame = new LoginFrame();
    loginFrame.setVisible(true);
    loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Notify every change to every bound property for that object:
    loginFrame.addPropertyChangeListener(this);
}

So, on my LoginFrame object I add a PropertyChangeListener . So I think that I am adding a mechanism for which when change the value of a property in this object it notify this change that will be handle by the following method (declared in my GUI class):

@Override
public void propertyChange(PropertyChangeEvent arg0) {
    System.out.println("GUI SingleFrameApplication ---> propertyChange(): " + arg0.getPropertyName());

    if (arg0.getPropertyName().equals("buttonLogOffClicked")) {
        //System.out.println("GUI SingleFrameApplication ---> richiamo exit");
        //exit();

        mainFrame.OnWindowClose();
        mainFrame.dispose();
        mainFrame = null;

        showLoginFrame();
    }

    if (arg0.getPropertyName().equals("loginResult")) {
        System.out.println("GUI SingleFrameApplication ---> richiamo MainFrame");
        //loginFrame.setVisible(false);
        loginFrame.dispose();
        loginFrame = null;

        showMainFrame();
    }

}

In the specific case in my LoginFrame class I have a JButton that if clicked fire the event that will be handled by the previous method, in this way:

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    System.out.println("Button LogIn cliccked");

  //  this.addPropertyChangeListener(listener);         // I add the PropertyChange Listener to this LoginFrame object

    // I fire a PropertyChange: the event start and will be handled by the propper propertyChange() method definied in the listener class: 

    firePropertyChange("loginResult", false, loginResult);   

}

Is it my reasoning correct?

Thanks

Andrea

  1. Instead of firing property change from Action performed function directly, better extends your target bean class, define a setXXX() method to change the xxx property. All Java beans is incorporating with getXXX() and setXXX() method to get and set their Property xxxx . The setXXX() is the one changing the property. we should fire the property, After we change the property, in the context we are changing it, hence it is the setXXX() method. Let us look into setPreferredSize(Dimesion) method source code of Component class :

      public void setPreferredSize(Dimension preferredSize) { if (prefSizeSet) { old = this.prefSize; } else { old = null; } this.prefSize = preferredSize; prefSizeSet = (preferredSize != null); firePropertyChange("preferredSize", old, preferredSize); } 

    See, we are firing the property upon changes of the property with the corresponding property name. The advantage is that it adds better clarity and make the code structure more readable.

  2. Rather than with conditional checking with each property while listening, i would like to use: addPropertyChangeListener("aProperty", PropertyChangeListener) method: which will listen to specific property changes as defined by in place of "aProperty" .

  3. As recommended by @Hovercraft below, the property name be a public String constant so as not to run into spelling or capitalization issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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