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

Recommended Answers

All 6 Replies

Do you mean something like the fireActionPerformed() method found in AbstractButton?

There is also a doClick() method on buttons.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.