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.
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:
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.
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:
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.
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
ActionListenercan 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
actionPerformedsmall and route to dedicated handler methods. - If the UI freezes, move long-running work off the Swing event dispatch thread.
Related reading
- Active threads in ExecutorService
- Activiti Spring Boot Gradle build hangs while gradle clean test
- Add context path to Spring Boot application
- Add context path to Spring Boot application
- Add external library .jar to Spring boot .jar internal /lib
- Add Header Value For Spring TestRestTemplate Integration Test
- Add leading zeroes to number in Java?
- add multiple cross origin urls in spring boot

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.