ActionListener
Java
Login
Button
Registration

ActionListener buttons only works for login or register, not both

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

When one Swing ActionListener seems to work for only one button, the problem is usually not that Swing can handle only one action. The real issue is normally how the event source is identified, how the buttons are wired, or how the listener branches between the two actions.

One Listener Can Handle Multiple Buttons

A single ActionListener can absolutely be attached to both a login button and a register button. The listener receives an ActionEvent, and that event tells you which component triggered it.

A reliable pattern is to give each button a stable action command:

java
1import java.awt.FlowLayout;
2import java.awt.event.ActionEvent;
3import java.awt.event.ActionListener;
4import javax.swing.JButton;
5import javax.swing.JFrame;
6import javax.swing.SwingUtilities;
7
8public class LoginRegisterFrame {
9    public static void main(String[] args) {
10        SwingUtilities.invokeLater(() -> {
11            JFrame frame = new JFrame("Auth");
12            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
13            frame.setLayout(new FlowLayout());
14
15            JButton loginButton = new JButton("Login");
16            JButton registerButton = new JButton("Register");
17
18            loginButton.setActionCommand("login");
19            registerButton.setActionCommand("register");
20
21            ActionListener listener = new ActionListener() {
22                @Override
23                public void actionPerformed(ActionEvent e) {
24                    switch (e.getActionCommand()) {
25                        case "login" -> System.out.println("run login logic");
26                        case "register" -> System.out.println("run register logic");
27                    }
28                }
29            };
30
31            loginButton.addActionListener(listener);
32            registerButton.addActionListener(listener);
33
34            frame.add(loginButton);
35            frame.add(registerButton);
36            frame.pack();
37            frame.setVisible(true);
38        });
39    }
40}

This avoids guessing based on button labels or relying on fragile UI text.

Separate Listeners Are Often Simpler

If the actions are unrelated, separate listeners are usually clearer.

java
loginButton.addActionListener(e -> System.out.println("run login logic"));
registerButton.addActionListener(e -> System.out.println("run register logic"));

That is often the best answer when the two buttons do completely different things. There is no prize for forcing both actions through one shared listener if separate lambdas are easier to read.

Common Reasons Only One Button Appears to Work

A frequent mistake is comparing button text instead of action commands or object references. If the visible label changes, the logic breaks.

Another mistake is reusing the wrong variable or accidentally attaching the listener to only one button.

This kind of bug is easy to miss in long setup methods:

java
loginButton.addActionListener(listener);
// registerButton never gets the listener

A third common problem is placing blocking work such as database access directly inside actionPerformed. The UI freezes, and it can look like the second button “does nothing” when the event thread is actually blocked by the first action.

A Better Design for Authentication Buttons

For login and register actions, the UI code should usually just dispatch the action and then call separate methods.

java
1private void handleLogin() {
2    System.out.println("validate credentials and log in");
3}
4
5private void handleRegister() {
6    System.out.println("validate fields and create account");
7}

Then the listener just routes to the correct method. That keeps the event code small and makes the real business logic easier to test.

If either action needs network or database work, trigger that work from a background task such as SwingWorker. The listener should stay responsive and update the UI only with short operations such as reading field values, disabling buttons, or showing validation messages.

Common Pitfalls

The biggest mistake is using button text as the identifier. Use setActionCommand(...) or compare component references instead.

Another mistake is wiring the listener to one button and assuming both buttons share it.

A third mistake is putting long-running work on the Swing event dispatch thread, which can make it look as if only one button responds.

Summary

  • A single ActionListener can handle both login and register buttons.
  • Use stable action commands or separate listeners instead of branching on button text.
  • Make sure both buttons actually receive the listener.
  • Keep actionPerformed small and route to dedicated handler methods.
  • If the UI freezes, move long-running work off the Swing event dispatch thread.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions