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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:42:42+00:00 2026-05-24T21:42:42+00:00

Is it possible to do this in a standard manner? Here is the scenario.

  • 0

Is it possible to do this in a standard manner?

Here is the scenario.

  1. Start doing something expensive in EDT (EDT is blocked till the expensive operation is over).

  2. While EDT was blocked, the user kept on clicking/dragging the mouse buttons. All the mouse actions are recorded somewhere.

  3. When EDT is free (done with the expensive stuff), it starts to process the mouse events.

What I want in step 3 is to discard the mouse events that have piled up. After the EDT is free, any new mouse event should be handled in the usual manner.

Any ideas on how to achieve this.

PS: It is not possible for me to prevent the EDT from getting blocked (I do not control the behavior of some of the modules in my program).

EDIT:
If I can safely call “SunToolkit.flushPendingEvents()” then I can always put a glasspane before starting the expensive operation in EDT. After the expensive operation is over then on the EDT thread, flush all the events – they will go to a glass pane that wont do anything. And then let EDT work as normal.

EDIT2:
I have added a SSCCE to demonstrate the issue.


public class BusyCursorTest2 extends javax.swing.JFrame {

    public BusyCursorTest2() {

        javax.swing.JButton wait = new javax.swing.JButton("Wait 3 seconds");
        getContentPane().setLayout(new java.awt.GridLayout(2, 1, 0, 0));
        getContentPane().add(wait);
        getContentPane().add(new javax.swing.JToggleButton("Click me"));
        setTitle("Busy Cursor");
        setSize(300, 200);
        setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);

        wait.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {

                final java.util.Timer timer = switchToBusyCursor(BusyCursorTest2.this);

                try {
                    //do something expensive in EDT
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        //do nothing
                    }
                } finally {
                    switchToNormalCursor(BusyCursorTest2.this, timer);
                }
            }

        });
    }

    public static java.util.Timer switchToBusyCursor(final javax.swing.JFrame frame) {
        startEventTrap(frame);
        java.util.TimerTask timerTask = new java.util.TimerTask() {

            public void run() {
                startWaitCursor(frame);
            }

        };
        final java.util.Timer timer = new java.util.Timer();
        timer.schedule(timerTask, DELAY_MS);
        return timer;
    }

    public static void switchToNormalCursor(final javax.swing.JFrame frame, final java.util.Timer timer) {
        timer.cancel();
        stopWaitCursor(frame);
        stopEventTrap(frame);
    }

    private static void startWaitCursor(javax.swing.JFrame frame) {
        frame.getGlassPane().setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
        frame.getGlassPane().addMouseListener(mouseAdapter);
        frame.getGlassPane().setVisible(true);
    }

    private static void stopWaitCursor(javax.swing.JFrame frame) {
        frame.getGlassPane().setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));
        frame.getGlassPane().removeMouseListener(mouseAdapter);
        frame.getGlassPane().setVisible(false);
    }

    private static void startEventTrap(javax.swing.JFrame frame) {
        frame.getGlassPane().addMouseListener(mouseAdapter);
        frame.getGlassPane().setVisible(true);
    }

    private static void stopEventTrap(javax.swing.JFrame frame) {
        frame.getGlassPane().removeMouseListener(mouseAdapter);
        frame.getGlassPane().setVisible(false);
    }

    private static final java.awt.event.MouseAdapter mouseAdapter = new java.awt.event.MouseAdapter() {
    };

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new BusyCursorTest2();
            }

        });

    }

    private static final int DELAY_MS = 250;

}
  1. Run the SSCCE

  2. Click on the button “Wait 3 seconds”. It simulates an expensive operation. The mouse cursor will change to busy.

  3. While the cursor is busy, click on the toggle button “Click me”. If after three seconds, the toggle button changes its state, then the mouse event was received by the toggle button and was not trapped.

I want that while the cursor looks busy, the generated mouse (and other) events be discarded.

Thanks.

  • 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-24T21:42:44+00:00Added an answer on May 24, 2026 at 9:42 pm

    OK, I finally got everything to work. I am posting the SSCCE for a correctly working example. The trick is to hide the glasspane using “javax.swing.SwingUtilities.invokeLater()” method. Wrap the necessary code in a Runnable and then invoke it using invokeLater. In such a case, Swing processes all the mouse events (nothing happens since a glasspane intercepts them), and then hides the glasspane. Here is the SSCCE.

    public class BusyCursorTest2 extends javax.swing.JFrame {
    
        public BusyCursorTest2() {
    
            javax.swing.JButton wait = new javax.swing.JButton("Wait 3 seconds");
            getContentPane().setLayout(new java.awt.GridLayout(2, 1, 0, 0));
            getContentPane().add(wait);
            getContentPane().add(new javax.swing.JToggleButton("Click me"));
            setTitle("Busy Cursor");
            setSize(300, 200);
            setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
            setVisible(true);
    
            wait.addActionListener(new java.awt.event.ActionListener() {
    
                public void actionPerformed(java.awt.event.ActionEvent event) {
    
                    final java.util.Timer timer = switchToBusyCursor(BusyCursorTest2.this);
    
                    try {
                        //do something expensive in EDT or otherwise
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            //do nothing
                        }
                    } finally {
                        switchToNormalCursorEventThread(BusyCursorTest2.this, timer);
                    }
    
                }
    
            });
        }
    
        public static java.util.Timer switchToBusyCursor(final javax.swing.JFrame frame) {
            startEventTrap(frame);
            java.util.TimerTask timerTask = new java.util.TimerTask() {
    
                public void run() {
                    startWaitCursor(frame);
                }
    
            };
            final java.util.Timer timer = new java.util.Timer();
            timer.schedule(timerTask, DELAY_MS);
            return timer;
        }
    
        public static void switchToNormalCursorEventThread(final javax.swing.JFrame frame, final java.util.Timer timer) {
    
            Runnable r = new Runnable() {
    
                public void run() {
                    switchToNormalCursor(frame, timer);
                }
    
            };
    
            javax.swing.SwingUtilities.invokeLater(r);
    
        }
    
        public static void switchToNormalCursor(final javax.swing.JFrame frame, final java.util.Timer timer) {
            timer.cancel();
            stopWaitCursor(frame);
            stopEventTrap(frame);
        }
    
        private static void startWaitCursor(javax.swing.JFrame frame) {
            frame.getGlassPane().setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
            frame.getGlassPane().addMouseListener(mouseAdapter);
            frame.getGlassPane().setVisible(true);
        }
    
        private static void stopWaitCursor(javax.swing.JFrame frame) {
            frame.getGlassPane().setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));
            frame.getGlassPane().removeMouseListener(mouseAdapter);
            frame.getGlassPane().setVisible(false);
        }
    
        private static void startEventTrap(javax.swing.JFrame frame) {
            frame.getGlassPane().addMouseListener(mouseAdapter);
            frame.getGlassPane().setVisible(true);
        }
    
        private static void stopEventTrap(javax.swing.JFrame frame) {
            java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue();
            frame.getGlassPane().removeMouseListener(mouseAdapter);
            frame.getGlassPane().setVisible(false);
        }
    
        private static final java.awt.event.MouseAdapter mouseAdapter = new java.awt.event.MouseAdapter() {
        };
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    new BusyCursorTest2();
                }
    
            });
    
        }
    
        private static final int DELAY_MS = 250;
    
    }
    

    Again, EDT if at all possible must not be blocked. But if you have to, you can have a working busy cursor as above.

    Any comments are welcome.

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

Sidebar

Related Questions

Is this possible? Or would I miss something? (Fixtures,..?) Because: When I use autotest
Is this possible? I've followed the instructions from here ( http://www.jetbrains.net/confluence/display/TCD4/Setting+up+an+External+Database#SettingupanExternalDatabase-MicrosoftSQLServer2005 ) but I
It's possible that this question either has a simple answer that's a standard practice,
Is this possible? I want to have the To:, Body, and an Attachment all
Is this possible? I am looking forward to a tutorial which explains the steps
Is this possible? I had troubles with SVN clients not being able to access
Is this possible? I can't find it anywhere.
Is this possible? I am creating a single base factory function to drive factories
Is this possible: myList = [] myList[12] = 'a' myList[22] = 'b' myList[32] =
Is this possible? example: $('a.change').click(function(){ //code to change p tag to h5 tag });

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.