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 7091235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:09:34+00:00 2026-05-28T08:09:34+00:00

I have a MouseListener Thread where this method is called each time there is

  • 0

I have a MouseListener Thread where this method is called each time there is a click:

public void mousePressed(MouseEvent event){

    //my Logic here

}

Now what happens is that there are so many clicks in little time, while my logic here, takes more time to process. What i was expecting is that further clicks will be discarded and this method will continue on the latest upcoming clicks.

But what is happening that while the logic is processing, the incoming clics are queued and even when clicking is stopped, qued clicks keep calling this mousePressed method, as a result i have multiple delayed executions.

Unfortunatelly i do not have control over the Listener (why it ques and keep sending delayed clicks), so in such a scenario, can you tell me what is the efficient way of handling such that i do not make a que due to delay in my processing.

I think the most obvious way would be using another thread, but i am wondering this might trigger so many threads in little time, or is there any way i can lock just one thread while the rest of clicks just go through empty loop?

  • 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-28T08:09:34+00:00Added an answer on May 28, 2026 at 8:09 am

    One solution would be to access the component on which the user clicked and disable it during the processing of the MouseListener. This way it doesn’t accept new clicks. This would have the additional benefit of giving the user visual feedback that he is not supposed to click on the component at this time.

    You can access the component through the MouseEvent:

    public void mousePressed(MouseEvent event) {
      event.getComponent().setEnabled(false);
      try {
    
        // ....
    
      } finally {
        event.getComponent().setEnabled(true);
    }
    

    In general though you shouldn’t do too much computation in the Swing event listener thread, because Swing also needs it for processing other events and painting. You should use separate Threads for doing actual work and just start them in the MouseListener. You can also use an ExecutorService to simplify this.
    It would still be a good idea to disable the gui component during the whole computation in order to give the user feedback.

    Edit:
    This solution of course depends on Swing. Any similar solution depends on the details of your GUI library, so if you use your own GUI library, you are one your own.

    You can of course use a general solution involving separate threads for doing the work. I really recommend an ExecutorService here, were you don’t need to care about the details and just submit tasks to execute. You can then have a simple volatile boolean variable which indicates whether the action is currently being executed and new clicks should be ignored.

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

Sidebar

Related Questions

I have declared a class as class DCFrameListener : public FrameListener, public OIS::MouseListener, public
I have a JTree with a MouseListener for Right-click detection. When the user clicks,
in a simplified version, what i have is this: public class MyLabel extends JLabel
Is there any way to add a MouseListener to a Graphic object. I have
So I have A MouseListener class inside a public class with a few methods.
I have a main window: public class MainPanel extends JFrame implements MouseListener { public
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you managed to get Aptana Studio debugging to work? I tried following this,
I have got a program having an array of buttons, each of these button
I have create a popup menu and on right click in a panel the

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.