• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ActionListener code. Problem deciphering of

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working through tutorial on event handling and got completely confused with the sample code. I understand the concept that a button eg gets an event from a user and then calls all its listeners' event handling method. Does a listener have a different handling method for each object it is listening to? The code below suggests it does but I can't see how you can implement an interface's ActionPerformed method with something called JButtonActionPerformed eg.

I don't understand the code which is supposed to do the registration. I can't see any connection between a particular component and a particular listener. There are 2 text boxes and a button set up. As I understand it they have a method to accept requests for 'registration' from listeners but I can't find any code where anything does that. I was expecting to see that in the parameter being passed but I can't.




and the corresponding ActionPerformed statements.




Perhaps ther's another example I can look at? ( Yes I have read Ch 12 in Head First Java.)
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

james dunster wrote:I am working through tutorial on event handling and got completely confused with the sample code.


Which tutorial? I suggest the Sun Swing tutorials on this subject, but in particular the non-NetBeans tutorial, the one that teaches you how to do Swing without the NetBeans-generated code.

Does a listener have a different handling method for each object it is listening to?


No, each ActionListener has one and only one actionPerformed method.

The code below suggests it does but I can't see how you can implement an interface's ActionPerformed method with something called JButtonActionPerformed eg.


No, in your sample code, you are creating two separate ActionListener objects anonymously, and each one has its own actionPerformed method, each one containing only one line of code. For instance here you create an anonymous ActionListener object whose actionPerformed method calls the method hoursWorkedActionPerformed():

This code not only creates the action listener object, but it also adds it to the hoursWorked (jbutton?) object. Perhaps this is what you mean by "registering".
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That'll teach you to let NetBeans design your GUI for you

You would have done far better to write the thing by hand! I often do. I have written about it in the past, try here.

What you have is an anonymous inner class, attached to each object in your display. I am sure this is a good object-oriented way to program, but you will have problems if you have several buttons which do similar things.

What you are doing is
  • 1: Registering a listener with the addXXXListener method.
  • 2: Creating a Listener object, here as an anonymous instance of the XXXListener interface, passed to that addXXX method.
  • 3: Implementing the method(s) of that XXXListener interface.
  • 4: Passing the activity to a method written elsewhere.
  • NetBeans or whatever tends to give the objects and methods rather awkward names like button1 button2 and listener3.

    When something happens to the Component, it sends off an XXXEvent object, which starts with the Component itself. If the Component has a Listener, that Listener catches the Event. Otherwise, I think, it passes to the "parent" ie Component enclosing. Then to whatever encloses that, etc etc., until it reaches the top-level Container. If it hasn't been caught by then, it vanishes into some sort of cyber-limbo never to be seen again. At least I think it does. Not sure.
    If the Event is caught, however, it invokes the method(s) of its Listener.
     
    james dunster
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    pete stein wrote:

    james dunster wrote:I am working through tutorial on event handling and got completely confused with the sample code.


    Which tutorial? I suggest the Sun Swing tutorials on this subject, but in particular the non-NetBeans tutorial, the one that teaches you how to do Swing without the NetBeans-generated code.

    OK I will look at that. I am reading Sun tutorial on getting started with NetBeans. It is confusing on this subject. It says that "the component must listen for an event" and you have to "register the component as a listener for that type of event" whereas I thought the component was the event source and indeed a diagram on the same page illustrates that and disagrees with the text.

    The NetBeans tutorial says that "The IDE automatically creates a so-called event listener for you, and hooks it up to your component." again suggesting that it is the component which does the listening.

    I now have plenty to read on the subject, thanks.

     
    james dunster
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:That'll teach you to let NetBeans design your GUI for you

    You would have done far better to write the thing by hand! I often do. I have written about it in the past, try here.

    ~snip


    I will read that. Seemed a good idea to get dozens of lines of code written for me but would be an even better idea if I understood what they mean. Back to long hand I think.

     
    So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic