6574

Getting the last resize event of a component

Question:

when using a ComponentListener to get notified of resize events, one can get a lot of events if the user drags the window corner to a new size.

Now, I need to do some expensive calculations on a resize event. That would be too much, if these occur that often. It would be OK to ignore most of the events and only react to the last one (if the user releases the mouse button, for example). The problem is, I do not know how to find out what the last resize event was. If I do the naive aproach:

this.addComponentListener(new ComponentAdapter() { @Override public void componentResized(final ComponentEvent e) { if (calculationInProgress){ return; } calculationInProgress= true; doExpensiveCalculation(); calculationInProgress= false; } });

it can happen that the window is resized even more after the last calculation and therefore the calculation wouldn't match the correct final dimensions.

What would a good solution to this?

Answer1:

Here's a simple example of the Timer-based solution discussed in the comments:

package mcve; import java.awt.event.*; import javax.swing.*; public abstract class ComponentResizeEndListener extends ComponentAdapter implements ActionListener { private final Timer timer; public ComponentResizeEndListener() { this(200); } public ComponentResizeEndListener(int delayMS) { timer = new Timer(delayMS, this); timer.setRepeats(false); timer.setCoalesce(false); } @Override public void componentResized(ComponentEvent e) { timer.restart(); } @Override public void actionPerformed(ActionEvent e) { resizeTimedOut(); } public abstract void resizeTimedOut(); }

And then:

comp.addComponentListener(new ComponentResizeEndListener() { @Override public void resizeTimedOut() { doExpensiveCalculation(); } });

Recommend

  • Passing code as parameter
  • Best way to expose cached objects to the views
  • Google Maps Decode Path (Polyline) - Uncaught SyntaxError: Unexpected token ILLEGAL
  • How to populate a data frame based on condition in R
  • Is using Bootstrap offset classes a correct method for centering elements?
  • Efficiently replacing negative values in large datasets conditional on type
  • Operate on every two columns in a matrix
  • Slicing an R vector
  • javascript retry promise until resolve
  • R: create new column with name coming from variable
  • Add blank rows in between existing rows
  • How can I keep midnight (00:00h) using strptime() in R?
  • Image slider
  • How to “extrapolate” values of panel data in R?
  • Click on button in another program - FindWindow, C#
  • Flash radiobutton: how do I get the selected radiobutton?
  • jQuery: How to AJAXify WordPress Search?
  • Selenium to click on a javascript button corresponding to a text
  • Button click event not firing in jQuery
  • How do I superscript characters in a UIButton?
  • onBackPressed() not being executed
  • GridView breaks while scrolling
  • Android fill_parent issue
  • NHibernate Validation Localization with S#arp Architecture
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Running a C# exe file
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • jquery mobile loadPage not working
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Cannot Parse HTML Data Using Android / JSOUP
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • JTable with a ScrollPane misbehaving
  • unknown Exception android
  • failed to connect to specific WiFi in android programmatically
  • Converting MP3 duration time
  • How can I use threading to 'tick' a timer to be accessed by other threads?