Results 1 to 15 of 15
Like Tree2Likes
  • 1 Post By RamyaSivakanth
  • 1 Post By Norm

Thread: Can the Action perform method be changed to add extra arguments to it

  1. #1
    wilnicm is offline Member
    Join Date
    May 2014
    Posts
    22
    Rep Power
    0

    Default Can the Action perform method be changed to add extra arguments to it

    Hi i was wondering two questions one which i think is a No. If it a "No" how do i create a custom one to be able to pass an extra argument

    Here is the below code
    Java Code:
            //Add action listener to button
            button.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                    System.out.println("You clicked the button");
                }
            });

    I want to be able to change it or create a custom one that will do this or something custom that will be able to perform the same thing but with an extra argument

    Java Code:
            //Add action listener to button
            button.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e, String Arg1)
                {
                    //Execute when button is pressed
                    System.out.println("You clicked the button");
                }
            });
    Thanks in advanced

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default Re: Can the Action perform method be changed to add extra arguments to it

    why you need to pass extra argument ?
    if u want to do something just call your method with extra argument inside the actionPerformed method.
    Tolls likes this.
    Ramya:cool:

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

    Default Re: Can the Action perform method be changed to add extra arguments to it

    I want to be able to change it or create a custom one that will do this
    How will the JVM know to call that method with that extra argument? The normal interface method it calls does not have that argument.
    gimbal2 likes this.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Can the Action perform method be changed to add extra arguments to it

    Exactly that. Its not you calling that method; its the Swing logic which does that when the user presses the button. So ask yourself this: where should the value for 'arg1' be coming from then?

    The answer to that question will likely tell you what you need to do in your actionlistener code instead of having an extra parameter. I can make a guess right now. Is it perhaps that you want to set a value ONCE when you create the action listener?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    wilnicm is offline Member
    Join Date
    May 2014
    Posts
    22
    Rep Power
    0

    Default Re: Can the Action perform method be changed to add extra arguments to it

    The problem i'm having is that i'm setting my string in one method and i want to send that string to my play button that is using the action listener. It was related to the problem yesterday i was having where the string i was using was coming in null to that method on the button clicked. The issue i found out was when i instantiated the object it was being null because i was instantiating it instead of sending it as an argument to that method. So now that i know what i think i need to do. I'm running against the problem of trying to send the string argument to the button clicked method that is using the action listener.

    Thats why i was acting is if there is a way around it for me to send that string argument to that button click method so i can use that value.

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

    Default Re: Can the Action perform method be changed to add extra arguments to it

    I don't know what that means. "Send a string to a button" ???

    You also don't answer my question: where is this value coming from? What are you going to put in 'arg' ?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    wilnicm is offline Member
    Join Date
    May 2014
    Posts
    22
    Rep Power
    0

    Default Re: Can the Action perform method be changed to add extra arguments to it

    Well the string gets set in one of my method. I wanted to call the method that the button is using but i cant call that method because its wanting an event argument. I wanted that method not only to accept event argument but also accept a string argument. So that is my delema. I was basically wondering if there is a way to do that. I couldnt figure that out with all the things that i did. There might be another way to look at it or view it that i'm not seeing or this i may not be able to do this at all in java.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Can the Action perform method be changed to add extra arguments to it

    You could overload the method for one without the event. You could pass a value of null for the event and check inside the method and act accordingly. You could rethink your overall design. Lot's of options here.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    wilnicm is offline Member
    Join Date
    May 2014
    Posts
    22
    Rep Power
    0

    Default Re: Can the Action perform method be changed to add extra arguments to it

    So currently it wont let me overload with putting a null for the event and sending the string nor will it allow me to add an action event and send my string?
    Here is the code below.

    Java Code:
           public void listTest(String cboxname) {
                       
                  
                    String result = cboxname;
                    this.listTest = result;
                     MainGui mygu = new MainGui();
                     mygu.playBtnActionPerformed(ActionEvent evte,cboxname);
                  //  System.out.println(this.listTest);
                }

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

    Default Re: Can the Action perform method be changed to add extra arguments to it

    send that string to ... the action listener
    Another idea: Have the action listener method call a method to get that String.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Can the Action perform method be changed to add extra arguments to it

    Is the playBtnActionPerformed your method? I can't tell what is going on from the code you submitted.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    wilnicm is offline Member
    Join Date
    May 2014
    Posts
    22
    Rep Power
    0

    Default Re: Can the Action perform method be changed to add extra arguments to it

    Yes sorry playBtnActionPerformed is the method i was trying to call to pass it the string.

    Norm but when i instantiate the object that the string lives in the playBtnActionPerformed, to get that strings, it seems to return null? wont it do the same thing in the action listener method also.

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

    Default Re: Can the Action perform method be changed to add extra arguments to it

    when i instantiate the object
    You should not create a new instance of the class(it won't have the data in it).
    Use a reference to an instance of the existing class that has the data you want.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    wilnicm is offline Member
    Join Date
    May 2014
    Posts
    22
    Rep Power
    0

    Default Re: Can the Action perform method be changed to add extra arguments to it

    Quote Originally Posted by Norm View Post
    You should not create a new instance of the class(it won't have the data in it).
    Use a reference to an instance of the existing class that has the data you want.
    Hi norm its me again. I don't know how to use a reference to an instance of the existing class. All the stuff i've read up online states that you cant technically pass data by reference and that you can do it only by value.
    Here is my current code that is returning an empty string when it gets back
    Java Code:
     private void playBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
            // TODO add your handling code here:
        Checkboxes.HandlerClass mhc = new Checkboxes().new HandlerClass();
        String selectTest ="";
        String myResult;
        myResult = mhc.getlistTest(selectTest);
            
            
    //        Checkboxes.HandlerClass mhclass = new Checkboxes().new HandlerClass();
    //        MainGui mg = new MainGui();
    
            userid = (String) loginUserCmbo.getSelectedItem();
            qaEnv = (String) eWhEnvCmbo.getSelectedItem();
    //        selectTest = mhclass.listTest;
    
            if (selectTest.matches("WMS Shell")) {
    
            } ......didnt finish writing remaining code



    Java Code:
    public String getlistTest(String cboxname) {
                       
                  String selectTest ="";
                    selectTest = cboxname;
                     
                    this.listTest = selectTest; 
                    System.out.println(this.listTest);
                     return this.listTest; 
    //                
                }
    I might be confusing what your saying with something else, so i apolozige in advance if this is the case and if its more simple then what i'm making out to be.

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

    Default Re: Can the Action perform method be changed to add extra arguments to it

    An example of passing a reference to an AClass object
    Java Code:
    class OneClass {
      SomeClass refToSomeClass = new SomeClass();
    
      public void aMethod() {
        AClass ac = new AClass();  // create instance and save address in ac
      ...  some other code here that could use ac
        refToSomeClass.someMethod(ac);  //  pass reference to the AClass object
       ...
    
    } // end OneClass
    
    //  In the class with the someMethod() method:
      class SomeClass {
       public void someMethod(AClass aCls) {
          // here aCls refers to the instance created above
         ...
    Last edited by Norm; 06-27-2014 at 09:28 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 12-12-2013, 07:08 PM
  2. Replies: 2
    Last Post: 12-11-2011, 10:44 PM
  3. Replies: 2
    Last Post: 02-17-2011, 12:07 PM
  4. My buttons all perform their action like 100 times
    By 711groove in forum New To Java
    Replies: 0
    Last Post: 12-13-2009, 10:49 AM
  5. Perform one action at a time
    By Melki in forum AWT / Swing
    Replies: 6
    Last Post: 12-08-2008, 07:29 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
  •