This is ClickMe program where it's suppose to change button text once the user clicks the button. Although, program does successfully launch and pops up the "Click ME" button but once it's clicked nothing happens, it doesn't change anything. Any help would be appreciated.

import javax.swing.*;
import java.awt.event.*;

public class MyResponse extends JFrame implements ActionListener 
{

    public static void main(String[] args)
    {
        new MyResponse();   
    }
    private JButton button;

    public MyResponse()
    {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(200,100);
        this.setVisible(true);
        this.setTitle("Click Program");
        JPanel panel = new JPanel();
        JButton button = new JButton ("Click ME!!");
        button.addActionListener(this);
        panel.add(button);
        this.add(panel);        
    }

    private int clickCount = 0;

    public void actionPerformed(ActionEvent e) 
    {
    if (e.getSource() == button)
        {   
        clickCount ++;
        if (clickCount == 1)
        button.setText("You just clicked me");      
        else
            button.setText(" You have clicked me " +clickCount+ "times!");

        }

    }

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You can't use "==" as a comparator on Objects. On Objects, "==" will only tell you if the two Objects are at the same memory location, I believe. In any case, it will not compare the two things for equality like it would if you said "does int 1 == int 2?"

edit: I'm looking into this a little more, but I tried it with '==' and it does work, because of how getSource works. I'll run your code and get back to you.

You are facing that problem simply because if we look inside your constructor, the "button", that you have added to your JFrame is this this one :-

JButton button = new JButton ("Click ME!!");

And you are adding the ActionListener on that button

Because of that the JButton object "button" declared at the class level is hidden in your constructor (to access it you would have had to use the "this" pointer).
And also in your actionPerformed() method when you are checking for the source of event, you are comparing it with the "button" object declared at the class level, (since it cannot see the "button" object declared in your constructor).


So whenever you are clicking your "button", the check e.getSource() == button fails, and whatever code inside the "if" block is never executed.

Please also remember to wrap your code inside [code=java] and [/code], It maintains indentations and provides syntax highlighting.

edit: I'm looking into this a little more, but I tried it with '==' and it does work, because of how getSource works. I'll run your code and get back to you.

Yes the "==" operator would work correctly here since e.getSource() would return you the reference of the original obect from where the event originated.

Thanks a ton! The problem has been solved. Thanks for your help :)

Oh, I found the problem. Change

JButton button = new JButton ("Click ME!!");

to

button = new JButton("Click ME!!");

Rather that just giving corrected code directly and making people dependent on you, I suggest you point the errors and then let them at least try to figure out the solution.

I usually do, but this is, in my opinion, a case where the person knows the concepts and just made an accidental error that they couldn't find. (In other words, by pointing out his mistake to him, he will understand why he made it).

This question has already been answered. Start a new discussion instead.