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 10 of 10

Thread: Can someone please explain this code for me?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Can someone please explain this code for me?

    @Override
    public void keyPressed(KeyEvent e)
    {
    if (e.getKeyChar() == 'a' ){
    String myS = myField.getText();
    myLabel.setText(myS);
    myLabel2.setText(“”);
    }
    }



    Can someone explain to me what this code actually does
    I'm trying to figure out what it does.

    Thanks

    Grot
    Last edited by Grot; January 17th, 2013 at 09:44 AM. Reason: wrap up code


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can someone please explain this code for me?

    Where did you get the code?
    When the method is called it makes a test of the contents of its arg and if the test is true it calls some methods.

    Read the API doc for the KeyEvent class and its method that is used to see what they do.

    (“”)
    I don't know what that is. It looks like definitions for a code pre-processing program.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can someone please explain this code for me?

    I got this code from a book but it didn't explain it, I edited it there so you can see it better
    Thanks for the help Norm! you're a legend!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can someone please explain this code for me?

    Now what I called pre-processor code has changed to slanting "s that aren't available on my keyboard.
    An empty String from my keyboard would look like: "" vs the “” in the posted code. I guess the pre-processor I mentioned is the one that formats the forum's replies.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can someone please explain this code for me?

    I think “” is supposed to be "".

    From when I see in this code is that
    if a key 'a' is pressed something happens
    and it creates a space.

    Am I right?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can someone please explain this code for me?

    if a key 'a' is pressed
    That is assuming there is a connection between key presses and the method. The code does not show there is any connection.

    it creates a space
    It calls a method (setText) with an arg of an empty String: "" (no space between the ""s). Read the API doc for the method to see what it does.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Can someone please explain this code for me?

    Quote Originally Posted by Grot View Post
    I think “” is supposed to be "".

    From when I see in this code is that
    if a key 'a' is pressed something happens
    and it creates a space.

    Am I right?
    Yes, it's common to cut and paste from an e-book and it ends up as “”.
    The code looks like it comes from a GUI, where it's looking for a key press (the event) and then it changes the labels on the GUI from a text field.
    Which book is it from?

  8. #8
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can someone please explain this code for me?

    What does the String myS = myField.getText(); do?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can someone please explain this code for me?

    It defines a String variable, calls a method and assigns what is returned by the method to the variable.

    Read the API doc for the method to see what is in the String that is returned.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    Grot (January 17th, 2013)

  11. #10
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can someone please explain this code for me?

    ok thanks Norm.

Similar Threads

  1. Could you explain this code to me please?
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 8
    Last Post: December 7th, 2011, 02:10 PM
  2. Don't know java ...Need help compare, explain 2 different versiosn of code.
    By asian2003k in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 9th, 2011, 12:41 PM
  3. Pls explain me the code logic
    By Shajith in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 02:41 AM
  4. Can anyone explain this code for me
    By gudwindavids in forum Object Oriented Programming
    Replies: 1
    Last Post: December 11th, 2009, 02:29 PM
  5. Help me this code! Someone please explain strings!
    By helpthiscode in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 16th, 2009, 03:13 AM