Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JButton; public class Main { /** * Attaches a key event listener to given component, simulating a button click upon * pressing enter within the context. * * @param context * @param button */ public static void simulateClickOnEnter(Component context, JButton button) { context.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) button.doClick(); } }); } }