Question about coalescing COMPONENT_RESIZED events

Jeremy Wood mickleness at gmail.com
Fri Apr 26 02:52:48 UTC 2024


I’m looking for general feedback/advice.

Is there any reason not to put the following method in a Component?

@Override
protected AWTEvent coalesceEvents(AWTEvent existingEvent, AWTEvent 
newEvent) {
     if (newEvent.getID() == ComponentEvent.COMPONENT_RESIZED)
         return newEvent;
     return super.coalesceEvents(existingEvent, newEvent);
}

(That is: is this unsafe/unwise for some reason I’m not considering?)

As I understand it: when you resize a window every call to 
component#setBounds(..) generates a new ComponentEvent and posts it to 
the event queue. So suppose you resized a window so its height was 10, 
then 11, then 12, then 13, then 14: that would create 5 ComponentEvents 
and post them to the event queue.

So suppose you also have this listener:

myComponent.addComponentListener(new ComponentAdapter() {
     public void componentResized(ComponentEvent e) {
         System.out.println(e.getComponent().getHeight());
     }
});

Your output in this scenario may be 10, 11, 12, 13, 14 if you have a 
very responsive EDT. But it could also be 14, 14, 14, 14, 14. The only 
guarantee is that the last printed height of the 5 ComponentEvents will 
be 14.

So if the ComponentEvents come in all at once: there’s no harm in 
coalescing them, right? (So now you may get anywhere from 1 to 5 
ComponentEvents, but each call to e.getComponent().getSize() will 
produce a different result.)

Regards,
  - Jeremy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/client-libs-dev/attachments/20240426/0f552254/attachment-0001.htm>


More information about the client-libs-dev mailing list