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

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).

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

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();
}

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 5 Years Ago by JamesCherrill: n/a

This question has already been answered. Start a new discussion instead.