?
Solved

Pass variable to ActionListener

Posted on 2004-04-30
7
Medium Priority
?
957 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

719 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