Allowing the Enter key to press the submit button, as opposed to only using MouseClick
There is a simple trick for this. After you constructed the frame with all it buttons do this:
frame.getRootPane().setDefaultButton(submitButton);
For each frame, you can set a default button that will automatically listen to the Enter key (and maybe some other event's I'm not aware of). When you hit enter in that frame, the ActionListeners their actionPerformed()
method will be invoked.
And the problem with your code as far as I see is that your dialog pops up every time you hit a key, because you didn't put it in the if-body. Try changing it to this:
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER){
System.out.println("Hello");
JOptionPane.showMessageDialog(null , "You've Submitted the name " + nameInput.getText());
}
}
UPDATE: I found what is wrong with your code. You are adding the key listener to the Submit button instead of to the TextField. Change your code to this:
SubmitButton listener = new SubmitButton(textBoxToEnterName);
textBoxToEnterName.addActionListener(listener);
submit.addKeyListener(listener);
Using Enter key to use JButton instead of just mouse click?
Check out Enter Key and Button for a discussion on this topic and a couple of solutions depending on your exact requirement:
- use the root pane to set a default button
- use the UIManager to have focus follow the button
- use Key Bindings to invoke the default Action
How to hit submit using the Enter key besides the mouseclick?
You can achieve this by calling setDefaultButton()
method of JFrame
's root pane. Try this example.
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.FlowLayout;
public class DefaultButton {
public static void main(String[] args) {
JButton button1 = new JButton("Button 1");
button1.addActionListener(e -> System.out.println("Button 1 action fired"));
JButton button2 = new JButton("Button 2");
button2.addActionListener(e -> System.out.println("Button 2 action fired"));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.getRootPane().setDefaultButton(button2);
frame.setBounds(300, 200, 400, 300);
frame.setVisible(true);
}
}
Trigger a button click with JavaScript on the Enter key in a text box
In jQuery, the following would work:
$("#id_of_textbox").keyup(function(event) {
if (event.keyCode === 13) {
$("#id_of_button").click();
}
});
$("#pw").keyup(function(event) { if (event.keyCode === 13) { $("#myButton").click(); }});
$("#myButton").click(function() { alert("Button code executed.");});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Username:<input id="username" type="text"><br>Password: <input id="pw" type="password"><br><button id="myButton">Submit</button>
Avoid only form submission on Enter key press
Replace your button with this button
<button type="button" onclick="formSubmit()">Submit</button>
then handler submit event with javascript like below.
function formSubmit() {
//here your code
}
How to submit a form using Enter key in react.js?
Change <button type="button"
to <button type="submit"
. Remove the onClick
. Instead do <form className="commentForm" onSubmit={this.onFormSubmit}>
. This should catch clicking the button and pressing the return key.
const onFormSubmit = e => {
e.preventDefault();
// send state to server with e.g. `window.fetch`
}
...
<form onSubmit={onFormSubmit}>
...
<button type="submit">Submit</button>
</form>
Related Topics
In Which Thread Do Completablefuture's Completion Handlers Execute
How to Get a Token from a Lucene Tokenstream
How to Use Collections.Sort() in Java
Performance Difference Between Java 8 Lambdas and Anonymous Inner Classes
Why Are Exceptions Not Checked in .Net
Gradle - Getting the Latest Release Version of a Dependency
Pdfbox - Signature Validity Checkmark Not Visible in Acrobat Reader
How to Zip a Folder Itself Using Java
When Does Java's Thread.Sleep Throw Interruptedexception
Difference Between Wait and Blocked Thread States
Automated Tests for Java Swing Guis
No Serializer Found for Class Org.Hibernate.Proxy.Pojo.Bytebuddy.Bytebuddyinterceptor