3

I'm using a simple ComponentAdapter to do something when the main JFrame window it's added to is resized. It's picking up the events without issue however I only want to act once a user has finished the resize. The componentResized() method fires multiple events for every pixel change of the resize and I don't want that as I need to scale an image when the window is resized and when it's done for every pixel it creates a huge lag.

I tried using a MouseListener on the frame to detect mouse up and down events to set a boolean as to whether it was being currently resized or not, but the events were never being triggered.

This is the simple ComponentAdapter:

private class ResizeListener extends ComponentAdapter {     
        public void componentResized(ComponentEvent e) {
            onResize();
        }
    }

And it is added to the frame in the constructor using this.addComponentListener(new ResizeListener()); The class is extending JFrame so it should be added to the frame. I tried using getContentPane().addComponentListener(new ResizeListener()); but that didn't make any difference.

Any advice on a simple or effective way of only running the componentResized() method when the window is actually finished resizing would be appreciated.

Goal

I'm implementing a PDF reader which at the moment converts the page being viewed into a BufferedImage. When the user resizes the window I need to appropriately scale the image which means I can't let layout managers look after that for me. The number of componentResized events creates a huge lag as the image is being resized for every position along the user's resize path so I need to do it once the resize is finished.

Community
  • 1
  • 1
LadyBernkastel
  • 447
  • 4
  • 13
  • 2
    Substitute something easier to render, such as @AndrewThompson's [marching ants](http://stackoverflow.com/a/9772978/230513). – trashgod Jan 04 '14 at 19:22
  • 2
    yes this idea is correct in the case that you want to avoiding the flickering on resize, because this event is fired per pixexl, but you have to use Swing Timer(local variable) with small delay 400-600, in componentResized(resize can continue) to call myTimer.restart(), if resize ended then Swing Action/Actionlistener will fire onResize(); – mKorbel Jan 04 '14 at 19:31

1 Answers1

5
Toolkit.getDefaultToolkit().setDynamicLayout(false);

This will affect all windows in the application, so if you only need this feature for a specific frame you may also want to add a WindowListener to the frame and then handle the windowActivated/windowDeactivated events to set this property.

camickr
  • 321,443
  • 19
  • 166
  • 288