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

  • Home
  • SEARCH
  • 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 528053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:56:49+00:00 2026-05-13T08:56:49+00:00

I would like to know if there exists in Java an event that is

  • 0

I would like to know if there exists in Java an event that is continuously fired when a mouse button is being held down (pressed), even if the mouse is not moved. I could not find it in typical MouseListeners:

  • MouseDragged is fired only if the user move the mouse while holding the button down
  • MousePressed is fired only once when the button has been pressed

And that’s about it.
Any idea how to make such an event?

Cheers

jy

  • 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-05-13T08:56:49+00:00Added an answer on May 13, 2026 at 8:56 am

    James Goodwin’s code will not work. mousePressed and mouseReleased are both fired from the GUI thread, so blocking in mousePressed will prevent mouseReleased from ever being fired, meaning the loop will continue forever.

    If you already have a seperate thread for processing then use mousePressed to indicate to that thread that the event should start, and mouseReleased to stop.

    If you don’t have a separate thread and don’t want the hassle, a Timer is probably the easiest way. javadoc on Timer.

    Specifically, you should create a TimerTask that does whatever it is you want to do multiple times and queue it using Timer.schedule:

    Timer timer = new Timer();
    TimerTask task = new MyTimerTask();
    
    private class MyTimerTask extends TimerTask {
        public void run() {
            // your code here
        }
    }
    
    public void mousePressed(MouseEvent e) {
        timer.scheduleAtFixedRate(task, 0, 1000); // Time is in milliseconds
        // The second parameter is delay before the first run
        // The third is how often to run it
    }
    
    public void mouseReleased(MouseEvent e) {
        task.cancel();
        // Will not stop execution of task.run() if it is midway
        // But will guarantee that after this call it runs no more than one more time
    }
    

    I’m pretty sure this is the simplest way, as it doesn’t involve faffing around with inter-thread communication.

    Oh, and as Peter said, you will have to add code to account for the user mousing down on your app and mousing up somewhere else.

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

Sidebar

Ask A Question

Stats

  • Questions 403k
  • Answers 403k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer func(temp<int>); There's no way to make the compiler infer the… May 15, 2026 at 5:17 am
  • Editorial Team
    Editorial Team added an answer Use the format string "img_%03d.jpg" to get decimal numbers with… May 15, 2026 at 5:17 am
  • Editorial Team
    Editorial Team added an answer Some of our conventions for using JIRA: Each project has… May 15, 2026 at 5:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.