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();
Pretty much. All you need to do is use the doClick() function. See the API for more information.
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.
Just call directly the actionPerformed() method from the ActionListenerimplementing class. You can do it the following way:
actionPerformed(new ActionEvent(gridButton2, ActionEvent.ACTION_FIRST, "youractioncommand"));
Related
Im making tic tac toe app, after first click on the button it get either "O" or "X" text and I want to avoid second click, like I want to make it immune to second click
if (!pola[i].getText().isEmpty()) {
pola[i].setEnabled(true);
}
so far I got this,
make a boolean clicked set it to true when the button is clicked, then in the action listener method check if !clicked before you execute the code, or maybe if u want u can do JFrame.remove(pola[i]); where JFrame is the frame you are using and if you want it to be interactable again you add it JFrame.add(pola[i]);
First of all I'm new to Java and you'll probably be able to guess. Thanks in advance.
I'm currently writing a board game using Java for an assignment. There are human and computer player classes, and the GUI only in the human player class. In the human class I use a class called 'UI' extending JFrame where the user selects a piece.
At the moment this UI class waits for the enter button to be pressed then sets a 'done' variable to true. In the human player class I create this UI then wait in a while loop continuously checking this boolean before getting the X/Y position of the move.
while (!input.moveEntered());
move.setPosition(input.getX(), input.getY());
This only seems to work if the while loop is not empty. If I add a print statement in there it works fine.
It seems like there is a much better way to go about this. I've looked into dialogs but I don't close the window every time a move is entered.
Thanks for reading.
You should never be busy-waiting; in your case, I would show a modal JDialog. When the user has entered their input, the setVisible(true) method will end without busy-waiting.
Or maybe I misunderstood your problem and you only need to define an event listener in your main JFrame in order to handle user input. Have a look at
http://www.tutorialspoint.com/swing/swing_event_listeners.htm
I am making a program for a friend of mine who is trying to break the world record in counting (yes, there is a world record for that). It's a simple program made in java with the swing library that is always on top (so that he can see it while he's gaming) with a simple JLabel in it that keeps track of how far he has counted. I added a hotkey that adds +1 to the counter in the JLabel every time he presses it.
My problem is that when the window is not selected i.e when you (if the program wouldn't be on top) otherwise would be tabbed out. The hotkey is not working. It seems you have to have the window focused for the keylistener to work.
Is there any way to get this to work?
Thanks in advance!
So, I was just wondering. Say I had a simple game of pong consisting of two JButtons as pongs that move with key input, a smaller JButton for the ball moving in a timer and a text box for a score. When I made this, I had to click on the button before I could move it. Is there a way to make it so that the button is selected by default when the program runs so that I can just press keys to move it straight away without clicking it first? Thanks.
Use the setDefaultButton method of the JFrame's root pane:
myFrame.getRootPane().setDefaultButton(button);
In frames constructor use below code after making button:
this.getRootPane().setDefaultButton(button);
I think you can select one by default with requestFocus().
Something like:
defaultJButton.requestFocus();
You should do that at the initialization or every time you want to restart, reset the state or similar.
I want to create a hidden button in netbeans. Suppose if I click a Next button then a new "Exit" button will be appear in the same jFrame. Until I click the Next button the Exit button will not be shown. So how to make it? I mean how to make a button which is by-default in .setVisible(false) in netbeans?
Please help me out about this problem.
Since you already seem to know the proper method I assume you doubt is about Netbeans operation.
Right click in the method and select "Customize Code".
Then you'll have the chance to add the proper code after the button creation.
Use JButton.setVisible(false); right after you create the button.