Solved

Pass variable to ActionListener

Posted on 2004-04-30
7
945 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 62
oracle 11g 23 106
Windows 10 IE Certificate Issue 10 51
Eclipse Java import and method not resolved 4 48
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

856 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