0

Ive gone through some stack overflow questions and found this similar question.

From what I understand using a switch statement in an actionPerformed method for this context will not work and an if-else statement is required.

Is there a more efficient way to do this without having repetitive code? I've heard I could use Abstract Action to give multiple buttons the same action but i haven't figured out how to use it properly.

@Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == loginButton){
            cardLayout.show(cards, LOGIN_PANEL);
        }
        else if(e.getSource() == signUpButton){
            cardLayout.show(cards, SIGN_UP_PANEL);
        }
        else if(e.getSource() == transactionHistoryButton){
            cardLayout.show(cards,TABLE_PANEL);
        }
        else if(e.getSource() == depositButton){
            cardLayout.show(cards, DEPOSIT_PANEL);
        }
        else if(e.getSource() == withdrawButton){
            cardLayout.show(cards, WITHDRAW_PANEL);
        }
        else if(e.getSource() == checkBalanceButton){
            cardLayout.show(cards,BALANCE_PANEL);
        }
        else if(e.getSource() == logout){
            cardLayout.show(cards, OPTION_PANEL);
        }
        else if(e.getSource() == backButtonP1){
            cardLayout.show(cards, OPTION_PANEL);
        }
        else if(e.getSource() == backButtonP2){
            cardLayout.show(cards, OPTION_PANEL);
        }
        else if(e.getSource() == backButtonP3){
            cardLayout.show(cards, UNLOCKED_PANEL);
        }
        else if(e.getSource() == backButtonP4){
            cardLayout.show(cards, UNLOCKED_PANEL);
        }
        else if(e.getSource() == backButtonP5){
            cardLayout.show(cards, UNLOCKED_PANEL);
        }
        else if(e.getSource() == backButtonP6){
            cardLayout.show(cards, UNLOCKED_PANEL);
        }
    }
  • This: *"From what I understand I cant use a switch statement in an actionPerformed method"* is inherently false. Where do you get this from? Please clarify your question with more details. You may have heard that you *shouldn't* do this, that "switchboard listeners" should be avoided, but that is a matter of design opinion and not a hard-and-fast rule. – Hovercraft Full Of Eels Jun 17 '21 at 01:02
  • If you look at the question i am referring to, multiple answers said that a switch statement is possible, but not in this use and that an if-else statement would need to be used, I will update the question to be more specific – Grewal_Creator Jun 17 '21 at 01:15
  • Again, your opening premise, that you ***can't*** use a switch in an ActionListener, is a false statement. You may wish to fix this part of your question. – Hovercraft Full Of Eels Jun 17 '21 at 01:17
  • 1
    For example, if you simply get the ActionEvent's actionCommand String, via `e.getActionCommand()`, you can easily do a switch statement with that. – Hovercraft Full Of Eels Jun 17 '21 at 01:18
  • Please look at [this answer to a similar question](https://stackoverflow.com/a/3827424/522444) that talks about restrictions on switch statement usage. – Hovercraft Full Of Eels Jun 17 '21 at 01:24
  • Possible duplicate of [Java switch statement: Constant expression required, but it IS constant](https://stackoverflow.com/questions/3827393/java-switch-statement-constant-expression-required-but-it-is-constant) – Hovercraft Full Of Eels Jun 17 '21 at 01:25
  • 1
    As for how to use AbstractAction please look at questions answered by [MadProgrammer](https://www.google.com/search?q=java+abstractaction+madprogrammer+site%3Astackoverflow.com), [camickr](https://www.google.com/search?q=java+abstractaction+camickr+site%3Astackoverflow.com) and [myself](https://www.google.com/search?q=java+abstractaction+hovercraft+site%3Astackoverflow.com) – Hovercraft Full Of Eels Jun 17 '21 at 01:32

1 Answers1

1

From what I understand using a switch statement in an actionPerformed method for this context will not work and an if-else statement is required.

Don't attempt to use a switch statement or nested if/else statements. This is an indication of poor design.

Is there a more efficient way to do this without having repetitive code?

If you want to share the same ActionListener for all your buttons then you would need to write a generic ActionListener.

Something like:

ActionListener al = new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        String command = e.getActionCommand();
        cardLayout.show(cards, command)
    }
}

Then when you create your buttons you would use:

JButton loginButton = new JButton("Login");
loginButton.setActionCommand(LOGIN_PANEL);
loginButton.addActionListener( al );

Or you can use a Java lambda to easily create a unique ActionListener for each button. Something like:

loginButton.addActionListener((e) -> cardLayout.show(cards, LOGIN_PANEL));

I've heard I could use Abstract Action to give multiple buttons the same action

You would use an Action, to give provide unique functionality. The benefit of an Action is that it can be shared by different components, like JButton or a JMenuItem, to perform the same Action.

Read the section from the Swing tutorial on How to Use Action for the benefits of using an Action over an ActionListener.

camickr
  • 321,443
  • 19
  • 166
  • 288