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.

Answer:

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.

Code:

import javax.swing.*;

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

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

f.setVisible(true);
}

public void windowResized(WindowResizeEvent e) {
System.out.println(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)
WINDOW_MAP.get(window);

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

public static void unregister(Window window, WindowResizeListener
listener) {
WindowResizeMonitor monitor = (WindowResizeMonitor)
WINDOW_MAP.get(window);

if (monitor != null) {
monitor.remove(listener);
}
}

private synchronized void add(WindowResizeListener listener) {
listeners.add(listener);

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

private synchronized void remove(WindowResizeListener listener) {
listeners.remove(listener);

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

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

try {
while (run) {
Thread.sleep(100);

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) it.next();
l.windowResized(event);
}
}
}

------------------------------------------------------------------------------------

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) {
super(source);
this.oldSize = oldSize;
this.newSize = newSize;
}

public void getOldSize() {
return oldSize;
}

public void getNewSize() {
return newSize;
}

<--- End of Code --->

Java Tips

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 © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com 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
www.erpgreat.com or the content authors.