e.getSource()
return an object. You are trying to compare it to a string. Instead you can use e.getActionCommand()
(assuming you haven't changes the action command)
Also in case you're temped, don't compare strings with ==
. Use equals
if ("ADD PERSON".equals(e.getActionCommand()) {}
Or if your buttons scope are accessble in the ActionListener
you can compare the object,
if (e.getSource() == addPersonButton) {}
Another option, in case for any reason you did change the action command you can also use the text of the button to compare
JButton button = (JButton)e.getSource();
String text = button.getText();
if ("ADD PERSON".equals(text)) {}
SIDE NOTE
- You should look into using a
CardLayout
that lets you change views. You say creating extra panels is time consuming, but debugging problems like this may be even more time consuming ;) See this simple CardLayout example and see How to use CardLayout