Detect That JFrame Is Being Resized

How does one detect that a JFrame is being resized? 
componentResized() of ComponentListener is only called when the mouse is released.


There is no mechanism in Swing supporting this, because the window resizing is handled by the underlying windowing system. It's of course possible to go to JNI and implement something, but here's a 100% java way to detect this. 

Note that this is a very rough implementation, for sure not production quality code.


import javax.swing.*;

public class WindowResizing implements WindowResizeListener {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(100, 100);

WindowResizeListener l = new WindowResizing();
WindowResizeMonitor.register(f, l);


public void windowResized(WindowResizeEvent e) {


import java.util.*;
import java.awt.Window;
import java.awt.Dimension;

public class WindowResizeMonitor implements Runnable {
private static final HashMap WINDOW_MAP = new HashMap();

private List listeners = new ArrayList();
private boolean run = false;
private Window window;

private WindowResizeMonitor(Window window) {
this.window = window;

public static void register(Window window, WindowResizeListener
listener) {
WindowResizeMonitor monitor = (WindowResizeMonitor)

if (monitor == null) {
monitor = new WindowResizeMonitor(window);
WINDOW_MAP.put(window, monitor);

public static void unregister(Window window, WindowResizeListener
listener) {
WindowResizeMonitor monitor = (WindowResizeMonitor)

if (monitor != null) {

private synchronized void add(WindowResizeListener listener) {

if (!run) {
run = true;
new Thread(this).start();

private synchronized void remove(WindowResizeListener listener) {

if (run && listeners.isEmpty()) {
run = false;

public void run() {
Dimension oldSize = window.getSize();

try {
while (run) {

Dimension curSize = window.getSize();
if (!oldSize.equals(curSize)) {
fireWindowResizeEvent(new WindowResizeEvent(window,
oldSize, curSize));

oldSize = curSize;
} catch (InterruptedException e) {

private void fireWindowResizeEvent(WindowResizeEvent event) {
Iterator it = listeners.iterator();

while (it.hasNext()) {
WindowResizeListener l = (WindowResizeListener);


import java.util.EventListener;

public interface WindowResizeListener extends EventListener {
void windowResized(WindowResizeEvent e);


import java.util.EventObject;

public class WindowResizeEvent extends EventObject {
private Dimension oldSize;
private Dimension newSize;

public WindowResizeEvent(Object source, Dimension oldSize,
Dimension newSize) {
this.oldSize = oldSize;
this.newSize = newSize;

public void getOldSize() {
return oldSize;

public void getNewSize() {
return newSize;

<--- End of Code --->

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of or the content authors.