KeyListener Or KeyBinding?

Hello,

I have a JDialog frame in which it is used to input/output information. I have a JButton called OK that I would to have handle an event when the user hits the Enter key. I want to allow the user to hit the Enter key no matter where the user has clicked as long as the dialog is in front. What would be more appropriate key listeners or key binding?

vyang

[380 byte] By [vyanga] at [2007-11-27 5:02:45]
# 1
dialog.getRootPane().setDefaultButton( okButton );
camickra at 2007-7-12 10:20:44 > top of Java-index,Desktop,Core GUI APIs...
# 2
Hello,Thanks camickr, that was much better and easier than binding or a listener. Now as a follow up question, would binding be a bad idea or is it that this way is much more efficient?vyang
vyanga at 2007-7-12 10:20:44 > top of Java-index,Desktop,Core GUI APIs...
# 3

You use whatever method in the API was designed to do what you want.

This is a common feature and so a special method exists to do this. Using a KeyBinding is a more general solution that will work, but why go to the extra effort when it is not required. The KeyBinding solution however, would be better then using a KeyListener.

camickra at 2007-7-12 10:20:44 > top of Java-index,Desktop,Core GUI APIs...