How to make a Java AWT Action Listener run multiple times



I have this code and I’m trying to have one action listener be used, and it be used multiple times. I have tested this code over and over again, and I have figured out that it is only running actionPerformed one time (When you click the “Login” button)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RyanOSGUI extends Frame implements ActionListener {
    //Login
    TextField password;
    Label passwordLabel;
    Button loginButton;

    //Apps
    Button console;
    Button calendar;

    //Calendar
    TextArea calendarView = new TextArea();
    Label addEvent = new Label("Add event to calendar: ");
    Label eventNameLabel = new Label("Event Name: ");
    TextField eventName = new TextField();
    Label eventDateLabel = new Label("Event Date: ");
    TextField eventDate = new TextField();
    Label eventTimeLabel = new Label("Event Time: ");
    TextField eventTime = new TextField();
    Button submitEvent = new Button("Submit event");

    RyanOSGUI(){
        password = new TextField();
        password.setBounds(175,90,100,20);

        passwordLabel = new Label("Enter Password:");
        passwordLabel.setBounds(175, 50, 100, 30);

        loginButton = new Button("Login");
        loginButton.setBounds(175, 140, 100, 30);
        loginButton.addActionListener(this);

        calendar = new Button("Calendar");
        calendar.setBounds(50,50,83,30);

        console = new Button("Console");
        console.setBounds(183,50,83,30);

        add(password);
        add(passwordLabel);
        add(loginButton);

        add(console);
        add(calendar);

        console.setVisible(false);
        calendar.setVisible(false);

        setResizable(false);
        setSize(450,500);
        setLayout(null);
        setVisible(true);
        setTitle("RyanOS");
        setIconImage(Toolkit.getDefaultToolkit().getImage("D:\Users\rperg\Pictures\Camera Roll\RyanOS.png"));
    }

    public void actionPerformed(ActionEvent a){
        //actionPerformed is only running once
        System.out.println("Ran action event");
        System.out.println(calendar);
        if(a.getSource() == loginButton){
            if(password.getText().equals("Testing123")){
                passwordLabel.setText("Welcome, Ryan!");
                setTitle("Ryan OS Home Screen");
                password.setVisible(false);
                passwordLabel.setVisible(false);
                loginButton.setVisible(false);
                console.setVisible(true);
                calendar.setVisible(true);
            }
            else{
                passwordLabel.setText("Wrong Password, Terminating program...");
                System.exit(69);
            }
        }
        if(a.getSource() == calendar){
            System.out.println("Ran Calendar App");
            console.setVisible(false);
            calendar.setVisible(false);
            runCalendar();
        }
    }

    public void runCalendar(){
        //Positioning
        calendarView.setBounds(10,10,430,300);
        addEvent.setBounds(10,310,100,30);
        eventNameLabel.setBounds(10,350,100,30);
        eventName.setBounds(110,350,100,20);
        eventDateLabel.setBounds(10, 380, 100, 30);
        eventDate.setBounds(110,380,100,20);
        eventTimeLabel.setBounds(10,410,100,30);
        eventTime.setBounds(110,410,100,20);
        submitEvent.setBounds(230, 375,100,30);

        //Adding to scene
        add(calendarView);
        add(addEvent);
        add(eventNameLabel);
        add(eventName);
        add(eventDateLabel);
        add(eventDate);
        add(eventTimeLabel);
        add(eventTime);
        add(submitEvent);
    }

    public static void main(String[] args){
        RyanOSGUI ros = new RyanOSGUI();
    }
}

class CalendarApp implements ActionListener {
    public void actionPerformed(ActionEvent a){

    }
}

Is there a way that I can fix this so that I can repeatedly use the actionPerformed method? I have tried doing different ways of making Action Listeners but I can’t seem to get them right (they return errors.)

Answer

it is only running actionPerformed one time (When you click the “Login” button)

loginButton.addActionListener(this);

You only ever add it to the “loginButton”.

You need to add it to your other buttons as well.

However, it is NOT a good design to share an ActionListener. A listener should only perform an single function.

I have tried doing different ways of making Action Listeners

Yes, creating a unique ActionListener for each button is the better approach.

for example:

loginbutton.addActionListener( (e) -> 
{
    System.out.println("login");
});


Source: stackoverflow