Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pass variable to ActionListener

Posted on 2004-04-30
7
Medium Priority
?
959 Views
Last Modified: 2008-03-06
hi all.   I need to pass a variable from a button that is clicked to the ActionListener.   Here's what I have:

this.viewButton[i].addActionListener(new viewAction(timeID.get(i).toString()));  //the button


//the action Listener inner class

    public class viewAction implements ActionListener{
        public viewAction(String id){
        }

        public void actionPerformed(ActionEvent e) {
            System.out.println("I'm clicked");
        }

    }


i just can't figure out how to get a 'String' into my ActionListener class.

Thanks.
0
Comment
Question by:calvinklein1
  • 4
  • 2
7 Comments
 

Author Comment

by:calvinklein1
ID: 10959916
sorry.  i should have stated that  'timeID' is an array that stores an 'Id' for each button in my panel.  So when the button gets click I want to grab the right 'Id' in my array and pass that to my 'ActionListener' class.   Just in case that was confusing.   If you want me to post the whole code I will.  It's just long and I wanted to keep this short.  Just let me know.  Thanks!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10959945
>>i just can't figure out how to get a 'String' into my ActionListener class.

you have a constructor that takes a String as parameter, so????

Don't understand
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 600 total points
ID: 10959967
public class viewAction implements ActionListener{
    String actionId; // member variable of action class
        public viewAction(String id){
              actionId = id; // <<<<< store the passed value
        }

        public void actionPerformed(ActionEvent e) {
            System.out.println("I'm clicked : ");
            System.out.println("Given id is: " + actionId); // <<<<<
        }
    }


Regards,
Muruga
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 400 total points
ID: 10959968
Oh, I see:

public class viewAction implements ActionListener{
        private String theId = null;            /// <<<<<<<<<<<<<<<

        public viewAction(String id){
           theId = id;      // <<<<<<<<<<<<<<<<<<<<<
        }

        public void actionPerformed(ActionEvent e) {
       
            System.out.println("I'm clicked: Id=", theId);  // <<<<<<<<<<<
        }

    }
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10959978
You see: I'm right. :)
0
 

Author Comment

by:calvinklein1
ID: 10960042
I thought I tried this and it didn't work.  but oh well.  my bad.  thanks for getting me back on track.   lol
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10960068
Thanks for accepting.
This keeps us answering your future questions too.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question