Is it possible to do this in a standard manner?
Here is the scenario.
Start doing something expensive in EDT (EDT is blocked till the expensive operation is over).
While EDT was blocked, the user kept on clicking/dragging the mouse buttons. All the mouse actions are recorded somewhere.
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; }
Run the SSCCE
Click on the button "Wait 3 seconds". It simulates an expensive operation. The mouse cursor will change to busy.
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.
EventQueue theQueue = getToolkit().getSystemEventQueue();
don't for get that if you send any events to the EDT manually, all waiting threads would be lost, and stays forever in the present form, simply don't do that, – mKorbel