0

Hi,

I currently have a method that is called on a ActionEvent of being clicked.

Now I want to call that method from another method.

Is there a way to do this with out having to just copy and paste my code into another method and just call that?

This is the method I want to call.

private void gameEndTurnBtnActionPerformed(java.awt.event.ActionEvent evt) {                                               
        if(theGame.WhosTurn() == 1)
        {
            theGame.SetWhosTurn(2);
            theGame.ResetMoved(theGame.Team2());
        }
        else
        {
            theGame.SetWhosTurn(1);
            theGame.ResetMoved(theGame.Team1());
            theGame.callAI();
            //want to call this method here. so as if someone had clicked the button to
            //trigger this event like normal
        }
        ClearSelected();
        ShowUnitInfo();
        UpdateScreenBoard();
        this.repaint();
    }

Thanks in advance

4
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • [QUOTE=ajst;1482134]Cheers JamesCherrill, thats exactly what i wanted, Thought it would throw exception if used null.[/QUOTE] It would throw an NPE exception IF you tried to use that parameter, but you don't. Passing null as an Object param is always valid syntax (unless it leaves you with an ambiguous method ref, … Read More

0

Since your method's code doesn't actually use the ActionEvent parameter, you could just call gameEndTurnBtnActionPerformed(null); from anywhere in your class (or anywhere else if you make it public).

0

Cheers JamesCherrill, thats exactly what i wanted, Thought it would throw exception if used null.

0

Oh here is the code incase anyone needs to know

private void gameEndTurnBtnActionPerformed(java.awt.event.ActionEvent evt) {                                               
        if(theGame.WhosTurn() == 1)
        {
            theGame.SetWhosTurn(2);
            theGame.ResetMoved(theGame.Team2());
            gameEndTurnBtnActionPerformed(null);
        }
        else
        {
            theGame.SetWhosTurn(1);
            theGame.ResetMoved(theGame.Team1());
            theGame.callAI(); 
        }
        ClearSelected();
        ShowUnitInfo();
        UpdateScreenBoard();
        this.repaint();
}
1

Cheers JamesCherrill, thats exactly what i wanted, Thought it would throw exception if used null.

It would throw an NPE exception IF you tried to use that parameter, but you don't. Passing null as an Object param is always valid syntax (unless it leaves you with an ambiguous method ref, which won't happen in this case).
cheers
James

Edited by JamesCherrill: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.