Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Click a JButton without the user Clicking it?

I have a JButton and when a player clicks it it tell my Action Listener that a button is clicked. What i want to know is that is there a command or something that acts as if a player clicked the button.

Like Tic Tac Toe, i have it so 2 players can play against each other, but i want to add the option for a computer player vs a human player. But since the computer cant actually click the button, i am lost.

Edit: would it be as easy as gridButton2.click() (Name of button).click();

like image 854
Joe C Avatar asked Apr 11 '11 23:04

Joe C


2 Answers

Pretty much. All you need to do is use the doClick() function. See the API for more information.

like image 119
Jeff Burka Avatar answered Sep 21 '22 02:09

Jeff Burka


for the tic-tac-toe thing, you don't need the computer to click a button. you just have to wait until the human makes a move, then have the computer choose its move and execute the code that happens if the button gets clicked.

like image 41
Andbdrew Avatar answered Sep 20 '22 02:09

Andbdrew