Solved

Pass variable to ActionListener

Posted on 2004-04-30
7
941 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 150 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 100 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now