Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: actionPerformed

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    18
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default actionPerformed

    Hi,

    I've made a button listen for actions.

    button.addActionListener(this);

    so, let's say the button is clicked twice, will actionPerformed() be invoked twice too ?


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: actionPerformed

    What happened when you tried it?

    db

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    18
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: actionPerformed

    Quote Originally Posted by Darryl.Burke View Post
    What happened when you tried it?

    db
    It gets invoked only once.. if it's a limitation, how can I work around it?

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: actionPerformed

    To get better help sooner, post a SSCCE that clearly demonstrates your problem.

    db

  5. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    18
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up Re: actionPerformed

    problem solved... thanks.
    Last edited by Kumarrrr; November 23rd, 2010 at 09:41 PM.

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: actionPerformed

    public void actionPerformed(ActionEvent e)
    {
    		int i = 1;
    		button.setText("I've been clicked "+ i + " number of times");
    		i++;
     
    // so, everytime the button is clicked, I want the button text to change.
     
    }

    To put it bluntly, you are wrong. The button does work each time you press it, but the value of i never changes. The reason is that each time you press the button, i get reinitialized to 1. This is also an issue of something called scope. Scope determines what parts of your code can see what other parts of your code. When you press the button once, i is set to 1, then the method increments it to 2. When you press the button the second time, the method cannot see the i from the previous method call so creates a new variable named i, sets it to 1, sets the button text, and increments i to 2. It will continue to do that each time you press the button.

    The fix to this is declaring your counter variable outside of the actionPerformed method. This should do it:
    int i = 1;
    public void actionPerformed(ActionEvent e)
    {
     
    		button.setText("I've been clicked "+ i + " number of times");
    		i++;
     
    // so, everytime the button is clicked, I want the button text to change.
     
    }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Kumarrrr (November 23rd, 2010)