Installs/resets a ComponentListener to resize the given window to minWidth/Height if needed : Various Event Listener « Event « Java






Installs/resets a ComponentListener to resize the given window to minWidth/Height if needed

 
/*
 * $Id: WindowUtils.java,v 1.16 2009/05/25 16:37:52 kschaefe Exp $
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.awt.Window;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentListener;

import javax.swing.SwingUtilities;

public class Utils {

  /**
   * Installs/resets a ComponentListener to resize the
   * given window to minWidth/Height if needed.
   *
   * @param window
   * @param minWidth
   * @param minHeight
   */
  public static void setMinimumSizeManager(Window window, int minWidth,
                                           int minHeight) {
      ComponentListener[] listeners = window.getComponentListeners();
      ComponentListener listener = null;
      for (ComponentListener l : listeners) {
          if (l instanceof MinSizeComponentListener) {
              listener = l;
              break;
          }
      }
      if (listener == null) {
          window.addComponentListener(new MinSizeComponentListener(
                  window, minWidth, minHeight));
      } else {
          ((MinSizeComponentListener) listener).resetSizes(minWidth,
                                                           minHeight);
      }
  }

  /**
   * Resets window size to minSize if needed.
   *
   * @author Patrick Wright
   */
  public static class MinSizeComponentListener extends ComponentAdapter {
      private Window window;
      private int minHeight;
      private int minWidth;

      MinSizeComponentListener(Window frame, int minWidth, int minHeight) {
          this.window = frame;
          resetSizes(minWidth, minHeight);
      }

      public void resetSizes(int minWidth, int minHeight) {
          this.minWidth = minWidth;
          this.minHeight = minHeight;
          adjustIfNeeded(window);
      }

      @Override
      public void componentResized(java.awt.event.ComponentEvent evt) {
          adjustIfNeeded((Window) evt.getComponent());
      }

      private void adjustIfNeeded(final Window window) {
          boolean doSize = false;
          int newWidth = window.getWidth();
          int newHeight = window.getHeight();
          if (newWidth < minWidth) {
              newWidth = minWidth;
              doSize = true;
          }
          if (newHeight < minHeight) {
              newHeight = minHeight;
              doSize = true;
          }
          if (doSize) {
              final int w = newWidth;
              final int h = newHeight;
              SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      window.setSize(w, h);
                  }
              });
          }
      }
  }
}

   
  








Related examples in the same category

1.Display the addXXXListener methods of any Swing classDisplay the addXXXListener methods of any Swing class
2.Show events as they happenShow events as they happen
3.A demonstration of the ChangeEvents generated by the BoundedRangeModelA demonstration of the ChangeEvents generated by the BoundedRangeModel
4.An application that shows the Action class in, well, actionAn application that shows the Action class in, well, action
5.Showing how to add Actions for KeyStrokesShowing how to add Actions for KeyStrokes
6.SketchSketch
7.EventListenerList enabled Secret LabelEventListenerList enabled Secret Label
8.TextAction exampleTextAction example
9.StateChange ListenerStateChange Listener
10.CheckBox Item ListenerCheckBox Item Listener
11.AncestorListener DemoAncestorListener Demo
12.KeyStroke SampleKeyStroke Sample
13.Focus Next Component SampleFocus Next Component Sample
14.PropertyChangeListener SamplePropertyChangeListener Sample
15.Timer SampleTimer Sample
16.KeyListener, ActionListener Demo 1KeyListener, ActionListener Demo 1
17.KeyListener, ActionListener Demo 2KeyListener, ActionListener Demo 2
18.Action, Mouse FocusAction, Mouse Focus
19.Swing Action DemoSwing Action Demo
20.Load Save ActionLoad Save Action
21.Demonstrating the WindowListener with a WindowAdapterDemonstrating the WindowListener with a WindowAdapter
22.Demonstrating the ActionListenerDemonstrating the ActionListener
23.Demonstrating the AdjustmentListenerDemonstrating the AdjustmentListener
24.Demonstrating the AncestorListener
25.Demonstrating the ComponentListenerDemonstrating the ComponentListener
26.A ComponentAdapter.
27.JAR Archives: Pack Progress Monitor
28.A position of a window on the screen.
29.Using ComponentListener to catch the JFrame Maximization event
30.Demonstrating the ContainerListenerDemonstrating the ContainerListener
31.Demonstrating the FocusListenerDemonstrating the FocusListener
32.Demonstrating the HyperlinkListenerDemonstrating the HyperlinkListener
33.Demonstrating the InternalFrameListenerDemonstrating the InternalFrameListener
34.Demonstrating the ItemListenerDemonstrating the ItemListener
35.Implement a graphical list selection monitorImplement a graphical list selection monitor
36.ItemListener for JRadioButton
37.Demonstrating the KeyListenerDemonstrating the KeyListener
38.Demonstrating the MenuListenerDemonstrating the MenuListener
39.Demonstrating the MouseListener and MouseMotionListenerDemonstrating the MouseListener and MouseMotionListener
40.Demonstrating the MouseWheelListenerDemonstrating the MouseWheelListener
41.Demonstrating the PopupMenuListenerDemonstrating the PopupMenuListener
42.Demonstrating the WindowListener
43.implements VetoableChangeListener to block focus change events
44.Responding to KeystrokesResponding to Keystrokes
45.Focus Event DemoFocus Event Demo
46.Container Event DemoContainer Event Demo
47.Component Event DemoComponent Event Demo
48.Window Event DemoWindow Event Demo
49.Handle a window closing event