my confirm button action listener is not printing the username and password even though I added a Actionlistener for it. please help thanks.

Codes

LoginDialog.java

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class LoginDialog extends JDialog {

    private JLabel nameLabel;
    private JLabel passwordLabel;
    private JTextField usernameTF;
    private JPasswordField passwordTF;
    private JButton confirmBtn;
    private JButton cancelBtn;
    private JPanel topPanel;
    private JPanel buttonPanel;
    private GridBagConstraints gbc;

    public LoginDialog() {
        this.setTitle("Authentication");

        topPanel = new JPanel(new GridBagLayout());
        buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        nameLabel = new JLabel("Name : ");
        passwordLabel = new JLabel("Password : ");
        usernameTF = new JTextField();
        passwordTF = new JPasswordField();
        confirmBtn = new JButton("Confirm");
        cancelBtn = new JButton("Cancel");
        buttonPanel.add(confirmBtn);
        buttonPanel.add(cancelBtn);
        gbc = new GridBagConstraints();

        gbc.insets = new Insets(4, 4, 4, 4);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        topPanel.add(nameLabel, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1;
        topPanel.add(usernameTF, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0;
        topPanel.add(passwordLabel, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 1;
        topPanel.add(passwordTF, gbc);

        this.add(topPanel);
        this.add(buttonPanel, BorderLayout.SOUTH);
    }

    public void showLoginDialog() {
        LoginDialog ld = new LoginDialog();
        ld.setSize(400, 150);
        ld.setVisible(true);
        ld.setLocationRelativeTo(null);
    }

    public String getUsername() {
        return usernameTF.getText();
    }

    public String getPassword() {
        return new String(passwordTF.getPassword());
    }

    public void confirmBtnListner(ActionListener listener) {
        confirmBtn.addActionListener(listener);
    }
}

Controller.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Controller {

    private LoginDialog loginDialog;

    public Controller(LoginDialog loginDialog) {
        this.loginDialog = loginDialog;
        loginDialog.showLoginDialog();
        loginDialog.confirmBtnListner(new BtnListener());
    }

    class BtnListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(loginDialog.getUsername());
            System.out.println(loginDialog.getPassword());
        }
    }

    public static void main(String[] args) {
        LoginDialog loginDialog = new LoginDialog();
        new Controller(loginDialog);
    }
}

You have TWO login dialogs - one created in main (line 24), the other created in showLoginDialog() (line 72).
The line 24 one gets the action listener, but the line 72 one is the one that is displayed, and that has no action listener.

This question has already been answered. Start a new discussion instead.