Results 1 to 10 of 10
  1. #1
    minime12358 is offline Member
    Join Date
    Jul 2010
    Posts
    21
    Rep Power
    0

    Default Using addKeyListener

    I was writing the following code:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.net.*;
    import java.awt.event.*;
       import javax.swing.*;
       import java.awt.image.*;
       import java.awt.Component;
    
    public class Plane extends Ball
    {
    ...
    public Plane()     
          {
    
              
             super();
             addActionListener(new Key());
    ...
          }
           public Plane(double x, double y)
          {
              super();
    addActionListener(new Key());
    ...
    
          }
              
    ...
          class Key extends KeyAdapter 
      {
       public void keyPressed(KeyEvent e)
       {
    ...
    
    }
    and I keep getting the error

    Cannot find symbol - method addActionListener(Plane.Key)
    any help would be great, thanks.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    minime12358 is offline Member
    Join Date
    Jul 2010
    Posts
    21
    Rep Power
    0

    Default

    Ohhh I see. wasnt realizing at the top that it had to be added to something

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    No, that's not the problem. The real issue is that a KeyAdapter is not an ActionListener.

    Perhaps you would like to describe your purpose in more detail. I say this because in most cases, a KeyListener isn't the best approach. Depending on the purpose, either KeyBindings or a DocumentFilter (or even a DocumentListener) are usually a better choice.

    db

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Darryl.Burke View Post
    either KeyBindings or a DocumentFilter (or even a DocumentListener) are usually a better choice.
    Interest on this Darryl, any comments?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Eranga View Post
    Interest on this Darryl, any comments?
    I think I can answer that one: the Listeners mentioned by Darryl handle much 'higher level' events than just a simple KeyEvent.

    kind regards,

    Jos

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Eranga View Post
    Means by 'higher level'?
    When something listens for keys the listener doesn't know how the key event will be handled by the component finally receiving and handling it (that's where those key bindings are for). So a key event handler has to fiddle diddle to decide what to do. It is much easier to listen for document changes or other 'higher level' events.

    kind regards,

    Jos

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    To elaborate: where a KeyListener can merely detect a Ctrl-V, a DocumentFilter/Listener can respond to the content of the text being pasted.

    And more about Key Bindings here:
    How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    db

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •