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

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Swing Event » ComponentListener 
15. 10. 6. 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 (instanceof MinSizeComponentListener) {
              listener = l;
              break;
          }
      }
      if (listener == null) {
          window.addComponentListener(new MinSizeComponentListener(
                  window, minWidth, minHeight));
      else {
          ((MinSizeComponentListenerlistener).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((Windowevt.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);
                  }
              });
          }
      }
  }
}
15. 10. ComponentListener
15. 10. 1. How to Write a Component ListenerHow to Write a Component Listener
15. 10. 2. ComponentListener and ComponentEvent
15. 10. 3. extends ComponentAdapter
15. 10. 4. A ComponentAdapter.
15. 10. 5. A position of a window on the screen.
15. 10. 6. Installs/resets a ComponentListener to resize the given window to minWidth/Height if needed
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.