Page 2 of 2 FirstFirst 12
Results 21 to 23 of 23
Like Tree1Likes

Thread: KeyListener?

  1. #21
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: KeyListener?

    The brackets are what enclose the body of the anonymous class. The above would be a compile time error since you have not provided the method implementations. And all listeners work pretty much the same. Key listeners take KeyEvents, Mouse listeners take MouseEvents, and ActionListeners (for buttons, etc) take ActionEvents. They only differ in what the Event passes and the number of methods each listener supports.

    And try this to clarify.

    Java Code:
    interface MyInterface {
       public void output();
    }
    
    MyInterface m = new MyInterface() {
        public void output() {
              System.out.println("Called from output in MyInterface()");
        }
    };
    
    m.output();
    I'm glad you got it working.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  2. #22
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: KeyListener?

    But... isn't it an object? I am not having a problem with the code, just understanding it. Maybe I wasn't taught well. I thought that doing something along the lines of KeyListener k = new KeyListener() just means making an object, which is why I was very surprise when brackets went after it. What I don't understand is why there are brackets after the creation of an object and why they need to have those three methods...
    thanks

  3. #23
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: KeyListener?

    Quote Originally Posted by MW130 View Post
    But... isn't it an object? I am not having a problem with the code, just understanding it. Maybe I wasn't taught well. I thought that doing something along the lines of KeyListener k = new KeyListener() just means making an object, which is why I was very surprise when brackets went after it. What I don't understand is why there are brackets after the creation of an object and why they need to have those three methods...
    thanks
    You've been told that those brackets are there because you're creating an anonymous inner class. What did you find out when you googled "java anonymous inner classes"?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Keylistener
    By dyelax in forum New To Java
    Replies: 4
    Last Post: 09-20-2012, 08:06 PM
  2. Need help with KeyListener
    By McDucky in forum New To Java
    Replies: 3
    Last Post: 09-16-2012, 06:35 PM
  3. Help with KeyListener
    By armyson in forum New To Java
    Replies: 1
    Last Post: 11-25-2011, 01:56 PM
  4. Help with keylistener?
    By Kaizo in forum New To Java
    Replies: 4
    Last Post: 12-11-2010, 01:55 AM
  5. keyListener not doing anything
    By imorio in forum AWT / Swing
    Replies: 10
    Last Post: 08-17-2010, 11:46 PM

Posting Permissions

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