Question

I am pretty new in Java Swing develompment and I am finding some difficulties using the PropertyChangeListener in my GUI.

So I have a Main class that implements PropertyChangeListener interface:

package com.test.login4;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFrame;


public class Main implements PropertyChangeListener {

    private static LoginFrame loginFrame;

    private static final GUI gui = new GUI();

    public static void main(String[] args) {
          System.out.println("Main ---> main()");   
          loginFrame = new LoginFrame();
          loginFrame.setVisible(true);


    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        System.out.println("Main ---> actionPerformed()");

    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        // TODO Auto-generated method stub
        System.out.println("GUI ---> propertyChange()");

    }

}

Then I have the LoginFrame class:

package com.test.login4;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;



public class LoginFrame extends JFrame implements ActionListener {

    private static final int FIXED_WIDTH = 550;
    private static final Dimension INITAL_SIZE = new Dimension(FIXED_WIDTH, 230);

    private boolean loginResult = true;

    public LoginFrame() {

        System.out.println("Inside LoginFrame ---> LoginFrame()");

        this.setTitle("XCloud Login");

        //this.setPreferredSize(INITAL_SIZE);
        this.setSize(INITAL_SIZE);
        this.setResizable(false);

        Container mainContainer = this.getContentPane(); // main Container into the main JFrame


        // JPanel creation and settings of the MigLayout on it:
        // JPanel externalPanel = new JPanel();
        JPanelWithBackground externalPanel = null;

        try {
            // externalPanel = new JPanelWithBackground("resources/logo.png");
            externalPanel = new JPanelWithBackground("src/com/test/resources/logo.png");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        externalPanel.setLayout(new net.miginfocom.swing.MigLayout("fill"));

        externalPanel.add(new JLabel("Username"), "w 50%, wrap");
        JTextField userNameTextField = new JTextField(20);

        externalPanel.add(userNameTextField, "w 90%, wrap");

        externalPanel.add(new JLabel("Password"), "w 50%, wrap");
        JTextField pswdTextField = new JTextField(20);
        externalPanel.add(pswdTextField, "w 90%, wrap");

        JButton loginButton = new JButton("Login");
        loginButton.setActionCommand("loginAction");
        loginButton.addActionListener(this);

        externalPanel.add(loginButton, "w 25%, wrap");

        mainContainer.add(externalPanel);
        // mainFrame.add(mainContainer);
        // loginFrame.setVisible(true);
        this.setVisible(true);
    }

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

        firePropertyChange("loginButtonClicked", false, true); 


    }

}

As you can see in this LoginFrame class when the user click on the JButton the actionPerformed method is executed (and it work, because I see it by the println) and in this method execute a firePropertyChange() method by this line:

firePropertyChange("loginButtonClicked", false, true); 

Then In the Main class I have the propertyChange() method that have to intercept this event but this seem don't work, because don't enter in this method and don't print "GUI ---> propertyChange()" in my console

Why? What am I missing?

Was it helpful?

Solution

You have to register the listener with addPropertyChangeListener, ie:

Main listener = new Main();
loginFrame.addPropertyChangeListener(listener);

See How to Write a Property Change Listener for details and example.

EDIT:

but where have I to insert your previuos 2 lines of code? in what class\method?

You do not allocate an instance of class Main in the posted sample, so I am not sure what is its purpose. The posted sample also does not compile. To see the output, add the following lines:

loginFrame.addPropertyChangeListener(new Main());

But it is simply a hack. All in all, you need to allocate an instance that implements PropertyChangeListener and register it with a panel using addPropertyChangeListener method.

EDIT:

Here is a sample that demonstrates a basic listener:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ListenerDemo {
    public static final String PROP_NAME = "loginButtonClicked";

    public ListenerDemo() {
        DemoPanel panel = new DemoPanel();

        panel.addPropertyChangeListener(PROP_NAME, new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                System.out.println(evt.getPropertyName());
            }
        });

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    class DemoPanel extends JPanel {
        public DemoPanel() {
            JButton button = new JButton("Test");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    firePropertyChange(PROP_NAME, false, true);
                }
            });
            add(button);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ListenerDemo();
            }
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top