So what I've been attempting to do is essentially have it set to when the enter key is pressed on my JFrame in java it detects it so I can do an action like start a game by switching to the next screen. So I was wondering if anyone could help me figure out how to do this in particular. Just being able to have something that constantly checks to see if the Enter Key has been pressed on the JFrame
have it set to when the enter key is pressed on my JFrame in java it detects it so I can do an action like start a game by switching to the next screen.
How do people know to use the Enter key?
Typically you would have a button like "Start Game" for the user to click.
You can easily make this the default button by using:
frame.getRootPane().setDefaultButton( startGameButton );
Now the user will be able to either:
click on the button
use the Enter key to invoke the button.
Related
I have started a project in Jframe Netbeans.
One thing which bothers me is this option where on one side I have actionPerformed which contains the action which we do when the key is pressed but again we have another option of key Pressed which does the same.
I know these two cannot perform the same function but what possibly could be the difference?
actionPerformed is called when the user triggers any event. It can be when a user clicks a button, selects a menu item, or presses enter in a text field.
Documentation:
https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
keyPressed is more specific and is for when a user presses a key. There are other methods available when implementing a KeyListener such as keyTyped or keyReleased which gives you more control over what the user is doing with the keyboard specifically.
Documentation:
https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
I'm writing a piece of software in Java, with a GUI programmed using swing. The user has to do this two operations in a loop:
write some words in a text field (let's call it A) and press Enter
click a button (let's call it B)
After Enter is pressed, some other fields show some data. After the button is clicked, all text fields are cleared and the procedure restarts. The text field to fill in is always A and the button to click is always B. This is what I would like to implement:
After the text field is filled and Enter is pressed, the button is somehow selected so that if the user presses Enter again the button is clicked
After the button is pressed, the text field is selected and in "write mode", so that the user can immediately start typing
What is the easiest method to implement this? Below I'm reporting what I have already tried.
I tried this to make the button selected immediately after Enter is pressed in the text field:
this.getRootPane().setDefaultButton(ButtonNextQuestion);
but when I press Enter, it is registered both by the text field and the button and I cannot see intermediate data.
I tried also with these lines:
ButtonNextQuestion.requestFocus();
ButtonNextQuestion.setSelected(true);
in this case everything seems working, but when I press enter the button is not clicked.
Thanks to AdamK!
The focus to the button was given like this:
ButtonNextQuestion.requestFocus();
ButtonNextQuestion.setSelected(true);
I had to add a listener to the button like this:
private void ButtonNextQuestionKeyPressed(java.awt.event.KeyEvent evt){
if(evt.getKeyCode() == 10) {//ENTER pressed: click
ButtonNextQuestion.doClick();
}
}
Then again to automatically select and make the text field ready to be written I used:
TextYourAnswer.requestFocus();
How to detect that pasting event is fired in the address bar through GWT? When we paste the thing from textbox to the addressbar then how to detect that pasting event in gwt.
What if a user decides to type an address instead of pasting it?
A standard approach is to use OnValueChangeEvent on a Textbox. It fires when a Textbox loses focus AND a value in the Textbox has changed, or when the Enter button is pressed while focus is still on the Textbox.
An alternative approach is to add a button, like "Take me there", that a user can press after a user is done entering the URL (either by pasting it or typing it).
I have a jTextField with two JButtons (Up arrow and Down arrow buttons). Clicking the Up arrow button the numeric value in the textfield increases by 1 (++), and clicking the Down arrow button the numeric value in the textfield decreases by 1 (--).
What I want know is how to automatically "scroll/change" the value while the button is pressed?
Thank you
What you probably want is a JSpinner. More specifically a SpinnerNumberModel.
Here is a link to a demo
http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html
The JSpinner is the best way to do this.
But if you want a different implementation, I would suggest using a MouseListener attached to the JButtons. When one of the button is pressed (the mousePressed event), a javax.swing.Timer is started. Every x milliseconds (depending on how fast you want the number increased/decreased) a check is made to see if the JButton is still pressed and if the mouse is still over the JButton. If it is, the number is increased/decreased. When the user releases the mouse (the mouseReleased event), the Timer is stopped/cancelled.
I never did this, so I don't know for sure that it works. But this is the way I would try 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();
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"));