|
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
{
WindowResizeListener l = new WindowResizing();
f.setVisible(true);
public void windowResized(WindowResizeEvent e) {
------------------------------------------------------------------------------------ import java.util.*;
public class WindowResizeMonitor implements Runnable {
private List listeners = new ArrayList();
private WindowResizeMonitor(Window window) {
public static void register(Window window, WindowResizeListener
if (monitor == null) {
public static void unregister(Window window, WindowResizeListener
if (monitor != null) {
private synchronized void add(WindowResizeListener listener)
{
if (!run) {
private synchronized void remove(WindowResizeListener
listener) {
if (run && listeners.isEmpty()) {
public void run() {
try {
Dimension curSize = window.getSize();
oldSize = curSize;
private void fireWindowResizeEvent(WindowResizeEvent event)
{
while (it.hasNext()) {
------------------------------------------------------------------------------------ import java.util.EventListener; public interface WindowResizeListener extends EventListener
{
------------------------------------------------------------------------------------ import java.util.EventObject; public class WindowResizeEvent extends EventObject {
public WindowResizeEvent(Object source, Dimension oldSize,
public void getOldSize() {
public void getNewSize() {
<--- End of Code ---> |
|
Do you have a Java Problem?
Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|