Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By jim829

Thread: setActionCommand() help

  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default setActionCommand() help

    I have:

    public void actionPerformed(ActionEvent evt)
    { String text = evt.getActionCommand();
    JTextField field = (JTextField)evt.getSource();
    String param = field.getText().toLowerCase();
    text = text.toUpperCase();


    if( text.equals("VISION") )
    {
    Parameter.VISION_MAX = Integer.parseInt(param);

    }


    ciVision = new JTextField(2);
    ciVision.setActionCommand("VISION");
    ciVision.setToolTipText("Max vision value");
    ciVision.addActionListener(this);
    Last edited by stuckonjava; 03-28-2014 at 01:57 AM.

  2. #2
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: setActionCommand() help

    I just want the user to enter a number in the text field and for it to update the variable in the Parameter class, I feel like I'm doing something wrong.

    Thanks

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,031
    Rep Power
    6

    Default Re: setActionCommand() help

    Well, first I suggest you put some print statements in your actionPerformed method. At a minimum, print out the actionCommand to see what it really is.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: setActionCommand() help

    Quote Originally Posted by jim829 View Post
    Well, first I suggest you put some print statements in your actionPerformed method. At a minimum, print out the actionCommand to see what it really is.

    Regards,
    Jim
    the thing I don't understand is how to get the relevant text field to assign to 'param', am I doing this correctly?

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,358
    Rep Power
    6

    Default Re: setActionCommand() help

    Easy to test.

    Java Code:
    JTextField field = (JTextField)evt.getSource();
    System.out.println(field.getActionCommand());
    If that prints VISION you're doing it correctly.


    The text value in it will apparently be a number, so I don't know why you are lowercasing it or expecting it to contain VISION.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,031
    Rep Power
    6

    Default Re: setActionCommand() help

    Quote Originally Posted by gimbal2 View Post

    The text value in it will apparently be a number, so I don't know why you are lowercasing it or expecting it to contain VISION.
    Good catch. Totally missed that.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: setActionCommand() help

    Quote Originally Posted by gimbal2 View Post
    Easy to test.



    If that prints VISION you're doing it correctly.


    The text value in it will apparently be a number, so I don't know why you are lowercasing it or expecting it to contain VISION.
    I don't understand, wouldn't text be assigned to "VISION" as this is what the action command has been set to for the field?

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,031
    Rep Power
    6

    Default Re: setActionCommand() help

    The action command is a String too which each component that supports actions may assign a value. It can be used to determine which JComponent fired the event. It has nothing to do with the actual text typed in the text field.

    You are setting the actionCommand correctly, but you are checking the text field. Two separate and unrelated strings.

    Also, the actionCommand is passed onto the event object so you can get the action command string via event.getActionCommand().

    Regards,
    Jim
    Last edited by jim829; 03-28-2014 at 02:50 PM.
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,358
    Rep Power
    6

    Default Re: setActionCommand() help

    Quote Originally Posted by stuckonjava View Post
    I don't understand, wouldn't text be assigned to "VISION" as this is what the action command has been set to for the field?
    Only if you type VISION as text into the field :s
    Last edited by gimbal2; 03-28-2014 at 03:07 PM. Reason: thank you jim
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: setActionCommand() help

    Quote Originally Posted by jim829 View Post
    You are setting the actionCommand correctly, but you are checking the text field. Two separate and unrelated strings.
    Is that right?
    Java Code:
    public void actionPerformed(ActionEvent evt) {
        String text = evt.getActionCommand();
        JTextField field = (JTextField)evt.getSource();
        String param = field.getText().toLowerCase();
        text = text.toUpperCase();
    
        if( text.equals("VISION") ) {
            Parameter.VISION_MAX = Integer.parseInt(param);
        }
    The 'text' variable is the ActionCommand.
    The 'param' variable is the contents of the text field.

    So that looks ok to me.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,031
    Rep Power
    6

    Default Re: setActionCommand() help

    Apparently it's OK. I was wrong. I was certain I saw a getText() in there. Which now goes full circle back to the OP's question. Why is this not working. Time for the print statements.

    @OP put print statements in the actionPerformed method before you test the command string. Make certain the command string and text string are what
    you expect. Sorry for all the confusion on this.

    Regards,
    Jim
    Last edited by jim829; 03-28-2014 at 07:11 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. ActionPerformed - JButton - How to setActionCommand?
    By SirVyvin in forum AWT / Swing
    Replies: 5
    Last Post: 12-24-2011, 02:07 AM
  2. JLabel .setActionCommand
    By stevemcc in forum AWT / Swing
    Replies: 1
    Last Post: 03-28-2008, 05:16 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
  •