samedi 11 février 2023

How to implement Observer Design Pattern in Java? [duplicate]

I am trying to implement observer in my project. I have created a test package and started very small and simple, without any interfaces and additional classes to see how it might work out. This is the code:

ConcreteSubject:

package observer;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class ConcreteSubject {

    private List<Component> component;

    public ConcreteSubject() {
        this.component = new ArrayList<>();
    }

    public void register(Component c) {
        component.add(c);
    }

    public void unregister(Component c) {
        component.remove(c);
    }

    public void unregisterAll() {
        component.removeAll(component);
    }

    public void newLogin() {
        loginPerformed();
    }

    private void loginPerformed() {
        JLabel errorLabel = (JLabel) component.get(0);
        JTextField usernameField = (JTextField) component.get(1);
        JTextField passwordField = (JTextField) component.get(2);
        if (usernameField.getText().equals("") || passwordField.getText().equals("")) {
            errorLabel.setForeground(Color.RED);
            errorLabel.setText("EMPTY FIELDS!");
            return;
        }
        errorLabel.setForeground(Color.GREEN);
        errorLabel.setText("YOU HAVE SUCCESSFULLY LOGGED IN!");
        usernameField.setText("");
        passwordField.setText("");
    }
}

Form:

package observer;

public class Form extends javax.swing.JFrame {

    ConcreteSubject subject;

    /**
     * Creates new form Form
     */
    public Form() {
        initComponents();
        subject = new ConcreteSubject();
        subject.register(error_Label_);
        subject.register(username_Field_);
        subject.register(password_Field_);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")                       

    private void login_Button_MouseClicked(java.awt.event.MouseEvent evt) {                                           

        subject.newLogin();
    }                                          

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Form().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel error_Label_;
    private javax.swing.JButton login_Button_;
    private javax.swing.JTextField password_Field_;
    private javax.swing.JLabel password_Label_;
    private javax.swing.JTextField username_Field_;
    private javax.swing.JLabel username_Label_;
    // End of variables declaration                   
}

Basically what i want to do in my real project is to be able to dynamically change components of jframe whenever a button is clicked or some object has changed.

And when i want to click on another button i want to change other set of components.

I don't know what might be the best way of implementing observer in this case.

Plus i am generally confused on how am i supposed to work around with netbeans gui builder.

Any help will be appreciated!

Aucun commentaire:

Enregistrer un commentaire