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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:22:24+00:00 2026-05-31T08:22:24+00:00

I am trying to emulate click events in Swing using following code: event =

  • 0

I am trying to emulate click events in Swing using following code:

event = new MouseEvent(target, MouseEvent.MOUSE_PRESSED, ...)
java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);
event = new MouseEvent(target, MouseEvent.MOUSE_RELEASED, ...)
java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);
event = new MouseEvent(target, MouseEvent.MOUSE_CLICKED, ...)
java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);

This works fine for most components, but the problem is if component is generating it’s own events, for example if when component receives MOUSE_PRESSED it generates some events and submits them with dispatchEvent(newEvent); With normal click event order would be:

MOUSE_PRESSED
newEvent
MOUSE_RELEASED
MOUSE_CLICKED

But because of my code order is:

MOUSE_PRESSED    
MOUSE_RELEASED
MOUSE_CLICKED
newEvent

And it breaks application logic. I can easily fix it by adding Thread.sleep() calls between my postEvent() calls, but I don’t want to do it since this method is called a lot and I don’t want it to be slow, especially since current code works in 95% of cases.

How would I emulate event sequence allowing new events to be created between them? I don’t have access to component code, so I can only modify my emulation method.

  • 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-31T08:22:26+00:00Added an answer on May 31, 2026 at 8:22 am

    You could push your own event queue which would give you total control of events. For example:

        class SchedulingEventQueue extends EventQueue
        {
          // Use Map< AWTEvent, List< AWTEvent > > to support multiple events
          private final Map< AWTEvent, AWTEvent > eventSchedule = new HashMap< AWTEvent, AWTEvent >();
    
          public void scheduleEvent( final AWTEvent event, final AWTEvent dependentEvent )
          {
            eventSchedule.put( dependentEvent, event );
          }
    
          @Override
          protected void dispatchEvent( final AWTEvent event )
          {
            try
            {
              super.dispatchEvent( event );
            }
            finally
            {
              // Dispatch any dependent event
              AWTEvent scheduledEvent = eventSchedule.remove( event );
              if( scheduledEvent != null )
              {
                postEvent( scheduledEvent );
              }
            }
          }
        }
    
        // Now the code to post the events becomes:
        {
          SchedulingEventQueue eventQueue = new SchedulingEventQueue();
    
          Toolkit.getDefaultToolkit().getSystemEventQueue().push( eventQueue );
    
          MouseEvent pressEvent = new MouseEvent(target, MouseEvent.MOUSE_PRESSED, ...)
          MouseEvent releaseEvent = new MouseEvent(target, MouseEvent.MOUSE_RELEASED, ...)
          MouseEvent clickEvent = new MouseEvent(target, MouseEvent.MOUSE_CLICKED, ...)
    
          eventQueue.scheduleEvent( clickEvent, releaseEvent );
          eventQueue.scheduleEvent( releaseEvent, pressEvent );
          eventQueue.postEvent( pressEvent );
        }
    

    You just chain the events together so that they don’t get posted until the previous event has been dispatched allowing any intermediate events created by a component to be posted during dispatch before your next event is posted. I haven’t tried this but it should work.

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

Sidebar

Related Questions

I'm using String.format() in Java trying to emulate the printf() control channel available in
I am trying to emulate a web browser in order to execute JavaScript code
I'm getting in trouble. I'm trying to emulate the call Application.Run using Application.DoEvents... this
I have a link that i am trying to emulate a click on. I
I'm trying to emulate the Safari / Chrome tabs using CSS (without using tables).
I'm trying to emulate the LIKE operator in LINQ to Objects. Here my code:
I'm trying to emulate a mousemove event in a link in IE8, but I'm
Is it possible to run a MySQL query using jQuery? I'm trying to emulate
I am trying to emulate a click on a basic link that appears like
I'm trying to emulate an inheritance model using django's generic relations . So far,

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.