Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9067577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:58:34+00:00 2026-06-16T16:58:34+00:00

I wanted to make a very simple click counter in Java. It works, but

  • 0

I wanted to make a very simple click counter in Java. It works, but every time when I stop clicking the Click Me button, number of clicks resets. I tried to solve this problem using static variable called clicks. I know this might sound like a dumb question, but how do you actually prevent the variable from reseting itself.

here is the code I wrote.

    package clickcounter;

import java.awt.BorderLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class ClickCounter extends JFrame implements MouseListener{
    private JButton b1 = new JButton("Click me");
    private static int clicks;
    private JLabel info = new JLabel();

    public ClickCounter()
    {
        super("Click counter");
        setSize(250, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        b1.addMouseListener(this);
        BorderLayout bor = new BorderLayout();
        setLayout(bor);
        info.setEnabled(false);
        add(BorderLayout.NORTH, b1);
        add(BorderLayout.CENTER, info);
        setVisible(true);
    }


    public static void main(String[] args) {
        ClickCounter cc = new ClickCounter();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        clicks = e.getClickCount();
        info.setText("Number of clicks " + clicks);
        info.repaint();
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // ignore
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //ignore
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // ignore
    }

    @Override
    public void mouseExited(MouseEvent e) {
       // ignore
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:58:35+00:00Added an answer on June 16, 2026 at 4:58 pm

    e.getClickCount() is used to provide details about the ‘click’. It helps applications to respond on double, triped, et.c clicks. So when the user stops clicking it is reset again.

    Replace

    clicks = e.getClickCount();
    

    with

    // *Add* the number of clicks that occurred to the click variable
    clicks += e.getClickCount();
    

    And your counter is not resetting anymore.

    Note: Making clicks static is not required in this situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn Java and I wanted to make a very simple class
I wanted to very quickly to make a simple drawing app for iOS, but
If you wanted to make a very simple file browser, let's say /some/folder on
Very new to javascript and html-type stuff. I wanted to just make a quick
I wanted to make a Java based web crawler for an experiment. I heard
I wanted to make some simple file recovery software, where I want to try
I wanted to make a button which is transparent until the user hovers over
I wanted to make a simple Countdown-Application with C# to show as an example.
I'm trying to make a simple website using mvc but I'm a little confused
I have a very simple Java code like this. I don't have any idea

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.