I'm having a hard time (as code gets longer) understanding how it flows when it's run. For example, with the program below what
is the actual algorithm or tree that is being followed when someone clicks "suite"? Looking at it I would think it would stop at
the JMenuItem but it obviously doesn't. Is the rest of the programmed scanned and it picks up on the menu(suite) item?
I think line 77 is throwing me off because I'm confused how you can create an Object right there and it applies
to everything else.
Ryan Bishop wrote:I think line 77 is throwing me off because I'm confused how you can create an Object right there and it applies
to everything else.
From the code you posted, line 77 is
Let's recap a bit on what the event delegation model is all about
1) Some actions trigger of events. e.g. Mouse move, mouse click, key typed, selection changed etc These events are automatically generated by the JVM
2) Typically you register a listener who will listen to such events
3) Depending on your requirement, you will process such events.
In your case, you achieved #2 by all those xxx.addActionListener(this); this being the SummervaleResort because it implements ActionListener
The actionPerformed() is the point where your listener gets notified about the events.
Since you have one single listener viz. SummervaleResort ready to listen to multiple events you need the ability to somehow figure out what triggered off the event. Imagine a dialog where you ask the user "Do you want to erase all your files?" with Yes and No buttons. Deleting files when the user clicked No (triggered the event) would be catastrophic.
Fortunately the designers of Java though of this problem and the JVM encapsulates the event source (which triggered off the event) inside the ActionEvent (the notification your listener receives), which you extract using the convenient getSource() method.
Now everything in Java is an object (except primitives like int of course) So the return type of the getSource is of Object type.
how you can create an Object right there
As you can see, you are not creating the object, but just referencing the one which was encapsulated inside the ActionEvent for you by the JVM