Hello, I need assistance with this java program I am writing, Im trying to make two buttons and when you click the button, it has a counter, and everytime you click it it prints a message saying I was clicked n times. This is what I have so far and I am pretty stuck. For example, I dont know how to assign a button a counter.

import java.awt.event.ActionListener;
 import javax.swing.JButton;
 import javax.swing.JFrame;

 public class ButtonViewer
 {
	 private static final int FRAME_WIDTH = 400;
	 private static final int FRAME_HEIGHT = 360;
	 int counter1 = 0;
	 int counter2 = 0;
	 public class ClickButtonClass implements ActionListener {
		public void actionPerformed(actionEvent cbc) {
			counter1++;
		}
	 }
	 
	 public static void main(String[] args)
	 {
		 //int counter1 = 0;
		 //int counter2 = 0;
		 
		 JFrame frame = new JFrame();
		 JButton button = new JButton("Click me!");
		 frame.add(button);
 
		 JFrame frame2 = new JFrame();
		 JButton button2 = new JButton("Click me too!");
		 frame2.add(button2);

		 ActionListener listener = new ClickListener();
		 button.addActionListener(listener);
		 button2.addActionListener(listener);
		 
		 //counter1++;
		 //counter2++;

		 frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 frame.setVisible(true);
		 
		 frame2.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		 frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 frame2.setVisible(true);
	 }
 }

And a sperate class file

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ClickListener implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent arg0) {

	}

}

what is your question?

why are you stuck, what is not working or what are you trying to implement next?

I am a beginner programmer in java so im not sure really what to do from here, I guess the next thing i wanna do with assign the button a counter. "This is what I have so far and I am pretty stuck. For example, I dont know how to assign a button a counter. "

when you click your button, you're not going to perform the actions in the ClickButtonClass because you are not adding that kind of Listener to the Button.
let your ClickListener class do the action you want to perform

and you'll need to take a closer look at how to build your frame. it might be easy to start off having your listener to be inside your frame class, since you want to use a variable in there, but you'll have to work out the static/instance issues

Edited 4 Years Ago by stultuske: n/a

you create 2 different classes that implement ActionListener, yet you added the one from the external file that has no code in it, instead of the internal one that has counter1++;

you actually don't have to create a different class to implement actionlistenner...

simply implement it from your main class, and overwrite "public void actionPerformed(..)" then when adding listenners to your buttons you can just say :

myButton.addActionListenner(this);
myOtherButton.addActionListenner(this);

then in your actionPerformed method simply check which button was clicked with :

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == myButton){
        //do stuff
        counter1++;
    }
    else if(e.getSource() == myOtherButton){
        //do stuff
        counter2++;
    }
}

Edited 4 Years Ago by Philippe.Lahaie: typo

This article has been dead for over six months. Start a new discussion instead.