Results 1 to 4 of 4
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [SOLVED] [newbie] Listener interface

    A listener object is an instance of a class that implements a special
    interface called (naturally enough) a listener interface.
    No such 'listener' was found in the API for 1.6 or 1.5

    :confused:

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    What 'listener' are we talking about?

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    It's an object that you create to handle events (e.g. mouse clicks, keyboard buttons pressed).

    Netbeans "autogenerated code" neatly adds something like this
    Java Code:
    jButton1.addActionListener(new java.awt.event.ActionListener()
    Seems like I have answered the question myself, I wasn't aware before what the book was "talking about".

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Yep, ActionListener is likely the most common "Listener" you'll use in Swing code. As your book suggests, it's an interface and you must create a class that implements it and then instantiate an object of that class before it can be used. NetBeans does this for you by creating what's called an anonymous inner class. So rather than creating a named class such as
    Java Code:
    Button1ActionListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        //... code goes here
      }
    }
    and then creating and using an object of this class, it creates an unnamed class and an object of this class at the same time with:
    Java Code:
    jButton1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button1ActionPerformed(e);
      }
    });

Similar Threads

  1. question on listener interface
    By Minu in forum Java Servlet
    Replies: 1
    Last Post: 01-16-2009, 10:33 AM
  2. Listener for print job
    By pjmorce in forum Advanced Java
    Replies: 1
    Last Post: 11-14-2008, 08:02 AM
  3. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-20-2008, 11:07 PM
  4. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 02:00 AM
  5. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM

Posting Permissions

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