This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes ActionListener code.  Problem deciphering of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ActionListener code.  Problem deciphering of" Watch "ActionListener code.  Problem deciphering of" New topic
Author

ActionListener code. Problem deciphering of

james dunster
Greenhorn

Joined: May 03, 2009
Posts: 20
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.)
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
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

    Joined: May 03, 2009
    Posts: 20
    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

    Joined: May 03, 2009
    Posts: 20
    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.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: ActionListener code. Problem deciphering of
     
    Similar Threads
    Reading RS232 and displaying contents on Frame
    java unable to delete from array list
    is it possible to let jComboBox display the object's string field in th DefaultComboBoxModel Vector
    Button Action using netbeans
    Insert an image in JScrollPane