Results 1 to 5 of 5
  1. #1
    nakkim is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default What is happening here?

    Hi, studying java to become an aspiring programmer, as a beginner programmer I need some simple interpretation of this code here:


    public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();
    if (source == speed) {
    // speed field caused event
    } else if (source == viscosity) {
    // viscosity caused event
    } else {source==start)
    // start caused event
    }
    }

    This is a code from the book I am studying from about "Event Handling", however I have trouble comprehending therefore would be grateful for simple explanation of this code.
    Last edited by nakkim; 02-11-2014 at 03:02 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: What is happening here?

    How about you tell us? What happened when you put together a little example program that used this code?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    nakkim is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Re: What is happening here?

    its a seperate piece of code, no main() this part of the lesson was about receiving "events" from "components" which are three objects above of JButtons, JTextFields.
    So the three components are receiving "events".
    Hm receiving events?
    So if source does not equal speed, or visocity, nor start it would not start the event? Sorry for the one dimensional way of thinking, if what I provided above gives no absolute sense than just ignore this post. Looks like I have multiple reading over to do again.
    Last edited by nakkim; 02-11-2014 at 03:09 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: What is happening here?

    You won't be able to really understand this code until you understand setting up a component and a listener. This code by itself does nothing, it has to be used in conjunction with other code that you haven't posted.

    More info here: Lesson: Writing Event Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: What is happening here?

    The event has already happened, probably as a result of user action on the UI; this code would just react to it in some way. Or perhaps not, if it's not the right type of event. This is called event driven programming. Google is your friend.

    Personally, I prefer to give each component (or group of components that do exactly the same thing) its own listener. Listeners with branching logic are ugly, IMO. But it's a matter of style and personal preference.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. NumberFormatException- shouldn't be happening?
    By RoKr93 in forum New To Java
    Replies: 5
    Last Post: 07-04-2013, 01:17 AM
  2. Replies: 2
    Last Post: 12-31-2011, 04:50 AM
  3. Replies: 1
    Last Post: 06-07-2011, 07:22 AM
  4. I don't know what is happening
    By Fred1 in forum New To Java
    Replies: 2
    Last Post: 04-27-2011, 11:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •