Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Newbie to any type of Programming

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Newbie to any type of Programming

    Hi all...I am 100% new to any type of programming. This is my first semester and I have been really working hard. It is going fairly well but I am still having a hard time understanding Triggers and Events. I have read my textbook over and over and searched the web like crazy(that's how I found this site). If anyone could help it would be great...


  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Newbie to any type of Programming

    Hello and welcome to the forums! What trouble are you having? Have you checked this site? http://download.oracle.com/javase/tutorial/uiswing/events/index.html

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Newbie to any type of Programming

    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...

  4. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Newbie to any type of Programming

    I guess a simple way to explain my problem is that I don't think I understand the difference between the two or how they actually work. I have a good handle on the rest of the chapter but I am stuck here and want to get this down before my Exam tomorrow.

  5. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Newbie to any type of Programming

    I think it means that a user clicking on a JCheckBox causes itemStateChanged.

    blue : trigger
    red : event

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Newbie to any type of Programming

    Quote Originally Posted by kcljeremy View Post
    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.

Similar Threads

  1. Illegal start of type?
    By mjpam in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 9th, 2010, 03:47 PM
  2. Not sure what type of variable to use.
    By mjpam in forum Java Theory & Questions
    Replies: 13
    Last Post: July 13th, 2010, 08:58 PM
  3. cannot be resolved to a type
    By Teraphim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 16th, 2010, 10:42 AM
  4. Newbie Programming problem
    By Pingu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:10 AM
  5. Java error "java.lang.StackOverflowError"
    By sanatkumar in forum Exceptions
    Replies: 2
    Last Post: July 7th, 2009, 02:40 PM