About 127 results
Open links in new tab
  1. java - Allowing the "Enter" key to press the submit button, as …

    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);

  2. java - How to use KeyListener - Stack Overflow

    Jun 4, 2012 · Create any object like JFrame object through which MyListener can listen the key events. for that you need to add MyListener object to the JFrame object. JFrame f=new JFrame(); f.addKeyListener(new MyKeyListener);

  3. java - Detect enter press in JTextField - Stack Overflow

    Jan 8, 2014 · Is it possible to detect when someone presses Enter while typing in a JTextField in java? Without having to create a button and set it as the default. A JTextField was designed to use an ActionListener just like a JButton is. See the addActionListener() method of JTextField. For example: @Override. public void actionPerformed(ActionEvent e)

  4. How to Write a Key Listener (The Java™ Tutorials - Oracle

    /** Handle the key typed event from the text field. */ public void keyTyped(KeyEvent e) { displayInfo(e, "KEY TYPED: "); } /** Handle the key-pressed event from the text field. */ public void keyPressed(KeyEvent e) { displayInfo(e, "KEY PRESSED: "); } /** Handle the key-released event from the text field. */ public void keyReleased(KeyEvent e ...

  5. How to Use KeyListener in Java - Delft Stack

    Feb 2, 2024 · Classes implement the KeyListener interface to listen and react to key events. In this tutorial, we learned a few important methods of the KeyEvent class. We also learned how to implement the KeyListener interface and how to override the keyPressed() , the keyReleased() , and the keyTyped() methods.

  6. Java KeyListener in AWT - GeeksforGeeks

    Apr 24, 2025 · The Java KeyListener in the Abstract Window Toolkit (AWT) is a fundamental tool for achieving this. The KeyListener Interface is found in "java.awt.event" package. In this article, we'll explore what the KeyListener is, and its declaration methods, and supply examples with explanatory comments.

  7. React to the ENTER key in a Textfield - Real's Java How-to - Real's HowTo

    TextField t = new TextField("press ENTER"); add(t); t.addKeyListener(this); public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_ENTER) { Toolkit.getDefaultToolkit().beep(); . System.out.println("ENTER pressed");

  8. How to Enable the 'Enter' Key to Trigger the Submit Button in a Java

    Create a simple Java Swing application with a JTextField and JButton. Implement an ActionListener for the JButton, and use a KeyListener on the JTextField to detect when the Enter key is pressed. Ensure the KeyListener triggers the …

  9. Simple key press listener - Java Code Geeks

    Nov 11, 2012 · In short, in order to implement a simple key listener in Java, one should perform these steps: Create a new class that extends KeyAdapter class. Override the keyPressed method to customize the handling of that specific event.

  10. Detecting when user presses enter in Java - Stack Overflow

    Sep 12, 2016 · You need to add the keyListener to the editor of the JComboBox and not the box directly: comboBox.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { if(evt.getKeyCode() == KeyEvent.VK_ENTER) { System.out.println("Pressed"); } } });

  11. Some results have been removed
Refresh