Originally Posted by
kcljeremy
my book gives the def of a trigger as: The impetus that causes the message to be sent that may come from another object or an external user. The def of an event is: the entire process of a trigger sending a message that causes an operation to occur.
I have read and reread the example it gives and It only confuses me. My assignment question list a display of a simple user interface with one drop down menu, one check box, and one group of radio buttons. It asks me to list the trigger and the event of each item.
To my understanding, I say that using the mouse to click the arrow on the drop down menu is the event and the trigger displays the items available in the menu.
Hopefully that will help a little...
Holy Shit. In what universe does a textbook publisher think that those definitions will make any sense to a beginner? Hell, I've programmed Java for two years and I just Events and Triggers all the time and those definitions still confuse the hell out of me.
Like Brt93yoda said, a Trigger is something that can be triggered (think of as a Noun maybe) and an Event is what will occur when that Trigger is activated (think of as the Verb for the Noun maybe).
I will get very technical, tell me if I loose you:
I assume you already know about Objects, Methods, and Inheritance. Well, all GUI items (or almost all of them anyway) inherit from the java.awt.Component Class. When a trigger is activated, a method is called in the background to deal with the trigger. For example, when you press a button on a GUI, the Component's
firePropertyChange method. This method is
protected (which means it isnt really supposed to be referenced by the programmer directly from another class) in the Component's Class and is what is triggered in the background. The
firePropertyChange method determines what sort of Event to call.
There are an insane amount of possible Events; but for Buttons, there is really only one that ever happens. That Event is called an ActionEvent (
java.awt.event.ActionEvent). Since the only thing that can really occur for Buttons is a state change (such as pressing the Button), when the Trigger is activated it calls the
fireStateChanged method it inherits from the javax.swing.AbstractButton Class instead of the
firePropertyChange method from the java.awt.Component Class. When programming, pretty much of all this is irrelevant because it all happens in the background.
But, the program does not know by default what to do when it receives that Event. So, you have to attach something called a Listener to the GUI Object, a JButton in our case. The Listener will instruct the program what to do in the situation when it receives a particular Event. For JButton, the method
addActionListener is inherited from the javax.swing.AbstractButton Class. This method accepts a parameter called a
ActionListener Object. The ActionListener Object is an interface that calls
actionPerformed(ActionEvent e) method. So, if you wanted to see the code at this point, it would look like this:
//Construct JButton
JButton button = new JButton("Button");
/* Create ActionListener and add to JButton.
* Since ActionListener is an Interface, we need to create a new Class that inherits ActionListener.
* The simplest way of doing this is to create it in the parameters of addActionListener.
* Notice how we Create a new ActionListener, provide a curly bracket ({) and inherit the actionPerformed Method betweent the brackets.
* Lastly notice how we have closed the addActionListener method after the ActionListener's closing curly bracket (})
*/
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
}
});
Now we have to tell the program exactly what we want it to do when the
actionPerformed method is called. For this example, we will make it print out the phrase: "Button Pressed." So, inside the actionPerformed method, we can type this code. It will look like this:
//Construct JButton
JButton button = new JButton("Button");
/* The Code inside the actionPerformed method will tell the JButton what to do when it is triggered.
* We are simply printing out: "Button Pressed."
*/
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Button Pressed.");
}
});
So, in this example, the Trigger is our JButton. When triggered, it will call its protect
firePropertyChange method. That will call the
addActionListener method we associated to the JButton. That will then call the
actionPerformed method we set for the ActionListener Object. That will then print out the phrase: "Button Pressed." For this example, our Event will be the ActionEvent Object that is sent to the
actionPerformed method in the background.
Tell me if that makes sense, I wrote it kind of fast and didnt compile it.