Results 1 to 9 of 9
  1. #1
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default is it possible to do this to methods?

    String gg = "KeyEvent.VK_1";
    robot.keyRelease(KeyEvent.VK_1);


    could i some how put the gg variable in replace so its like robot.keyRelease(g);

    Now i know the above code doesnt work, but is there a way like this to do it?
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by alacn View Post
    String gg = "KeyEvent.VK_1";
    robot.keyRelease(KeyEvent.VK_1);


    could i some how put the gg variable in replace so its like robot.keyRelease(g);

    Now i know the above code doesnt work, but is there a way like this to do it?
    If you feel you're up to it, use reflection; read all about it in the API documentation for the Class and Field classes.

    kind regards,

    Jos

  3. #3
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    oh i see, good news. Im loving Javas flexibility to be honest.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default

    Since keyRelease takes an int, not a String, you could code it this way:
    int gg = KeyEvent.VK_1;
    robot.keyRelease(gg);

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Norm View Post
    Since keyRelease takes an int, not a String, you could code it this way:
    int gg = KeyEvent.VK_1;
    robot.keyRelease(gg);
    But what to do if the OP only has a String value like "VK_1" or "VK_A"? The answer could be (gasp) reflection.

    kind regards,

    Jos

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default

    Sorry, I don't see how reflection converts a String to an int.
    Sounds more like a javac function, but that is into byte code which could be returned as an int via a method call.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Norm View Post
    Sorry, I don't see how reflection converts a String to an int.
    Sounds more like a javac function, but that is into byte code which could be returned as an int via a method call.
    Java Code:
    Field field= KeyEvent.class.getField("VK_1");
    int value= field.getInt(null);
    That's what reflection is all about ;-) I only use it as a last resort.

    kind regards,

    Jos

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default

    Thanks for that.

    That's my new thing learned for today.
    Last edited by Norm; 08-14-2010 at 02:37 PM.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Norm View Post
    Thanks for that.

    That's my new thing learned for today.
    Something new on a Saturday? Oh dear, better forget all about it then because reflection (or even worse: the Introspector object) doesn't play the game by the rules. You can do anything you like with class objects or classes itself with that stuff. It makes you part of the virtual machine and breaks every protection or access right you'd normally have. Besides that, it's more expensive to use than ordinary object use; it's a poweful mechanism and extremely dangerous in the hands of newbies ...

    kind regards,

    Jos

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Methods Please help
    By bdario1 in forum New To Java
    Replies: 33
    Last Post: 03-25-2010, 03:26 AM
  3. methods
    By lilac87 in forum New To Java
    Replies: 7
    Last Post: 07-22-2009, 06:37 PM
  4. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  5. methods
    By Zensai in forum New To Java
    Replies: 10
    Last Post: 12-03-2007, 05:31 AM

Posting Permissions

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