Question

Is it possible to save pending events from event queue and then restore them?

There is following workflow: user click on JTable row and there is a check if row is changed, depending on that check modal confirmation window is opened. When user double clicks row detail view is opened. So when modal window is opened it swallows all mouse events and thus double click does not reach JTable.

I wanted somehow save all pending events from event queue before opening modal window and restore them after modal window is closed.

Following I tried

EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
Stack<AWTEvent> events = new Stack<AWTEvent>();
PaintEvent pe = new PaintEvent(DetailPanel.this, PaintEvent.PAINT, new Rectangle(10, 10));
eventQueue.postEvent(pe);
while (eventQueue.peekEvent() != null) {
    AWTEvent event = eventQueue.getNextEvent();
    events.push(event);
}
..... // modal window code
while(!events.isEmpty()) {
    eventQueue.postEvent(events.pop());
}

This does not help, double click does not reach JTable.

Also I tried pushing whole queue:

EventQueue stackQueue = new EventQueue();
EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
stackQueue.push(eventQueue);
... //modal window code
eventQueue.push(stackQueue);

In this case programs seems to be deadlocked.

Any suggestions are appreciated.

Was it helpful?

Solution

Seems I found issue. There should not be Stack, just ordinary FIFO collection.

So following schema correctly saved and restored events

EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
List<AWTEvent> events = new ArrayList<AWTEvent>();
eventQueue.postEvent(new PaintEvent(DetailPanel.this, PaintEvent.PAINT, new Rectangle(10, 10));
while (eventQueue.peekEvent() != null) {
    events.add(eventQueue.getNextEvent());
}
..... // modal window code
For (AWTEvent event : events) {
    eventQueue.postEvent(event);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top