Depending on where you want to trap the “enter” key, you could use an ActionListener
(on such components such as text components or buttons) or attach a key binding to you component
public class MyPanel extends JPanel { public MyPanel() { InputMap im = getInputMap(WHEN_FOCUSED); ActionMap am = getActionMap(); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "onEnter"); am.put("onEnter", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { // Enter pressed } }); } }
This will rely on the component being focused.
Related Posts:
- How to use Java AWT setBackground
- Difference between java.exe and javaw.exe
- Getting a HeadlessException: No X11 DISPLAY variable was set
- Simple Java 2D graphics: draw a rectangle?
- Resizing image in Java
- JOptionPane YES NO OPTION
- Java: Difference between the setPreferredSize() and setSize() methods in components
- addMouseListener for a JPanel
- How do you add an ActionListener onto a JButton in Java
- How to make PopUp window in java
- JFrame in full screen Java
- Java: Custom Buttons in showInputDialog
- Keylistener not working for JPanel
- java.awt.EventQueue.invokeLater explained
- Setting background color for a JFrame
- Adding a Scrollable JTextArea (Java)
- Problem with gif with transparent background
- How change the color arrowIcon to JMenu when it is selected
- error upon assigning Layout: BoxLayout can’t be shared
- How do I draw a triangle?
- Java: Rotating Images
- How do I draw a triangle?
- repaint() Method in Java
- What does “AL lib: alc_cleanup: 1 device not closed” mean?
- Java GUI: about getContentPane( ) method and content
- How to center the text in a JLabel?
- How to add an image to a JPanel?
- How to repaint a JPanel after have drawn on it?
- Implementing an actionlistener to a JTextField
- How to Set JPanel’s Width and Height?
- Java Wait for thread to finish
- Swing/Java: How to use the getText and setText string properly
- What does super.paintComponent(g) do?
- How to Set the Background Color of a JButton on the Mac OS
- Java Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
- Popup Message boxes
- Exception in thread “AWT-EventQueue-0”?
- make a JLabel wrap it’s text by setting a max width
- setMnemonic() and call a method by pressing the key
- the getSource() and getActionCommand()
- How to programmatically close a JFrame
- How does addNotify() and requestFocus() work in Java with JPanel?
- DTO and DAO concepts and MVC
- Adding JPanel to JFrame
- Adding image to JFrame
- How to Retrieve value from JTextField in Java Swing?
- What does .pack() do?
- Setting background images in JFrame
- Java Swing revalidate() vs repaint()
- How does paintComponent work?
- How can I set size of a button?
- Java Swing setting JPanel Size
- How to draw a circle with given X and Y coordinates as the middle spot of the circle?
- JOptionPane Yes or No window
- Exception in thread “main” java.util.NoSuchElementException
- “Error occurred during initialization of VM; Could not reserve enough space for object heap” using -Xmx3G
- What’s the simplest way to print a Java array?
- javax vs java package
- What is a stack trace, and how can I use it to debug my application errors?
- How to fix “A JNI error has occurred, please check your installation.”
- java – invalid method declaration; return type required [duplicate]
- Java optional parameters
- typeof in Java 8
- What does the arrow operator, ‘->’, do in Java?
- Switch on Enum in Java
- Converting JSON data to Java object
- Why do this() and super() have to be the first statement in a constructor?
- Else without if
- Java – String cannot be converted to int
- restart console on Minecraft Server
- SSL peer shut down incorrectly in Java
- Javamail Could not convert socket to TLS GMail
- java.net.SocketException: Connection reset
- Cannot instantiate the type Queue. Why is this?
- How do I instantiate a Queue object in java?
- Pause the timer and then continue it
- Comparing chars in Java
- Converting String Array to an Integer Array
- How to fix “unsupported class file major version 60” in IntelliJ?
- Java “params” in method signature?
- Resize an Array while keeping current elements in Java?
- Syntax error on token(s), misplaced construct(s)
- JAVA Variable declaration not allowed here
- How to check if object is null or not except == null
- How do I check if the user is pressing a key?
- How to check if object is null or not except == null
- Comparing strings by their alphabetical order
- exposed beyond app through ClipData.Item.getUri
- Java – get the current class name?
- Java: How To Call Non Static Method From Main Method?
- What does idempotent method mean and what are the side effects in case of calling close method of java.lang.AutoCloseable?
- How to “wait” a Thread in Android
- How to use regex in String.contains() method in Java
- Official reasons for “Software caused connection abort: socket write error”
- Multiple assignment at once in java
- -XX:MaxPermSize with or without -XX:PermSize
- Decompile .smali files on an APK [duplicate]
- How to get rows and columns count of a 2D array in Java?
- Spring Boot – Cannot determine embedded database driver class for database type NONE
- Convert double to float in Java