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?