Solved

Pass variable to ActionListener

Posted on 2004-04-30
7
943 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 98
Oracle SQL syntax check  without executing 6 54
login jsp example 24 49
How do I remove an object from a 3 25
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.

773 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