Question

I have a doubt related to the deep meaning and use of the fireIndexedPropertyChange() method that fire an event that will be intercepted and handled by a propertyChange() listener that I declare in another class.

For example, in a class that implement a login GUI (that show a login form) with a JButton I have this method that is performed when the user clik on my button:

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

    Main listener = new Main();                         // I intantiate a new Main object to have the PropertyChangeListener
    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: 
     * 
     * @param "loginButtonClicked" 
     * */
    firePropertyChange("loginResult", false, loginResult);   


}

So, when the not yet logged user click on my button this method is performed and it fire a new PropertyChange event.

My PropertyChange event take itself the following informations:

  1. The propertyName that is a name of a property that could be changed (in my example is named loginResult because I have a boolean variable named loginResult and the value is true if the user can log in when clikc the button, false otherwise

  2. An old value is the old value of the property that could be changed (in my example is false because if the user see the login windows it means that it is not logged in and the loginResul=false

  3. The new value: in this case it is the content of the loginResult variable (true if the user is being logged in the system)

So this PropertyChange event will be intercepted by my propertyChange() method (declared in a listener class) that handle it based on these parameters: for example in the following way:

** if the propertyName is loginResult and if the new value is true then don't display again the login window but display the main application window**

Is it my reasoning correct and it could be a good way to use the PropertyChangeListener interface?

Tnx

Andrea

Was it helpful?

Solution

You can use it like that and it will work. But I think your question is more about semantics rather than just whether it works. The package of that interface is java.beans which puts it in a context of beans. If you are using it in a beans context, i.e. your event publisher is a bean then I think the "deep meaning" of the interface agrees with your use. The javadoc says

A "PropertyChange" event gets fired whenever a bean changes a "bound" property.

If you don't think it goes well with beans then create your own

OTHER TIPS

There is no reason to use events to implement your use-case. You're overengineering simple things. The code should simply look like the following:

@Override
public void actionPerformed(ActionEvent e) {
    String login = loginTextField.getText();
    char[] password = passwordField.getPassword();
    boolean loginSuccessful = authenticationService.authenticate(login, password);
    if (loginSuccessful) {
        Main main = new Main();
        main.setVisible(true);
        this.setVisible(false);
    }
    else {
        displayErrorMessage("Login failed");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top