Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Certain keyboard functions that I needed to understand to make work an applet.

 
Nikhilesh Puranik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Ranchers!
I'd like to know what the function addKeyListener(this); do for a keyboard event applet Java.
Also, what does the requestFocus(); do for the same? How would they work together when we invoke them?
 
Partheban Udayakumar
Ranch Hand
Posts: 496
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhilesh Puranik,

element.addKeyListener(this) (as the name suggest) will add a key listener to the element.

element.requestFocus() will change to focus to the element you have called it on.

Please tell us the details for what and how you want to use them together.


 
Nikhilesh Puranik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:Nikhilesh Puranik,

element.addKeyListener(this) (as the name suggest) will add a key listener to the element.

element.requestFocus() will change to focus to the element you have called it on.

Please tell us the details for what and how you want to use them together.




Okay. I actually wanted to know how they both work in an applet that handles keyboard events. I am studying for my upcoming MCA Java lab exam tomorrow. I'll post the program code here that my college lab manual provided.

P.S., I also wanted to know whether the code in comment braces also will get executed, considering that this program will be compiled as an applet. Thanks in advance.

 
Partheban Udayakumar
Ranch Hand
Posts: 496
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhilesh Puranik wrote:
Okay. I actually wanted to know how they both work in an applet that handles keyboard events.


I think requestFocus() is given here in order to focus the applet.
Handling keyboard events is purely done by KeyListener.

For more details on keyListeners, look at this link. It also contains things about focus in it.

Nikhilesh Puranik wrote:P.S., I also wanted to know whether the code in comment braces also will get executed, considering that this program will be compiled as an applet.


You are right. The code will work, as it is an applet.
 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code shown addKeyListener adds the AppletKeyboard instance as a listener of keyboard events. This means whenever keyboard keys are pressed and/or released a keyboard event will be sent to the instance ie one or more of the methods keyPressed(), keyReleased and/or keyTyped() will be called.

requestFocus() requests that this gui component (ie the applet) becomes the focused component and hence will receive keyboard events. Note however that this is only a request and whilst in all liklihood the component that requests focus will recieve focus this is not guaranteed and other components/applets/applications could request focus after the your applet in which case they would become the focus for keyboard events.
Having said all of that, I'm not sure how necessary this line of code is in this case as I would have thought once the applet is started it would automatically be the focused component. You could always comment it out and see if the applet still works.
 
Nikhilesh Puranik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic