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) {
	 public static void main(String[] args)
		 //int counter1 = 0;
		 //int counter2 = 0;
		 JFrame frame = new JFrame();
		 JButton button = new JButton("Click me!");
		 JFrame frame2 = new JFrame();
		 JButton button2 = new JButton("Click me too!");

		 ActionListener listener = new ClickListener();

		 frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		 frame2.setSize(FRAME_WIDTH, FRAME_HEIGHT);

And a sperate class file

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

public class ClickListener implements ActionListener {

	public void actionPerformed(ActionEvent arg0) {


6 Years
Discussion Span
Last Post by mKorbel

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 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 :


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

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == myButton){
        //do stuff
    else if(e.getSource() == myOtherButton){
        //do stuff

Edited by Philippe.Lahaie: typo

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.