Using ComponentAdapter to determine when Frame Resize is finished

1.2k views Asked by At

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.

1

There are 1 answers

1
camickr On BEST ANSWER
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.