Error with KeyPress Events

I am testing for the enter keypress in a JformatedField date.  I want when the user press enter on this date field "sodate" it calls a calendar - CalendarTyp2().  However, I am getting an error on "KeyEvents" in the line - public static class KeyEvents implements KeyListener.  Can you help me to resolve this error as to what I could be doing wrong?   For further analogy, I delared the method as KeyEvents ke = new KeyEvents(); and add the component as sodate.addKeyListener(ke)

public static class KeyEvents implements KeyListener {
		
		 public void keyPressed(KeyEvent e) {
			 if(e.getKeyCode()==10) {
		          new CalendarType2();
			 }
		}
	}

Open in new window

Vanavah EdwardsAsked:
Who is Participating?
 
for_yanCommented:


You need to implement not just one but all methods of KeyListner

Thats why the error om implements KeyListener line
0
 
for_yanCommented:

 public void keyPressed(KeyEvent e) {
// something here
}

public void keyTyped(KeyEvent e) {
// maybe nothing here but themethods needs to be
}


public void keyReleased(KeyEvent e) {
// maybe nothing here but themethods needs to be
}
0
 
Vanavah EdwardsAuthor Commented:
Okay I will do.  However, I is there a key adapter that can be use where you only need one but all methods would be accessible.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
for_yanCommented:

 >For further analogy, I delared the method as   KeyEvents ke = new KeyEvents();

Not understandable what the above line  means,
 but th eerror becasue you need to impleemnt all methods
0
 
for_yanCommented:
There may be, but no sense remebering it - it is simpler to paste the methods in your code
0
 
Vanavah EdwardsAuthor Commented:
YOu are right it works.  Thank you.  Thank you  very much.  I will now close this session.
0
 
for_yanCommented:
0
 
Vanavah EdwardsAuthor Commented:
One more thing, must this procedure be done for other listener events.
0
 
for_yanCommented:
Each listener interfcae has its own set of methods.
Say, ActionListener has just one method actionPerformed  you'll normally implement it
But those which have sveeral methods - all need to be implemented - like KeyListener, MouseListener
But your IDE usually will remind you which method is missing - you hoever over red line on your mistake-
it will give you method name to implemnet, then if still red hover again, etc.
until you have all methods implemented, _MouseListnere has 5 of them
0
 
Vanavah EdwardsAuthor Commented:
Thank you very much again.
0
 
for_yanCommented:
You are always welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.