Java JComponent Size restrictWindowMinimumSize(final Window wnd, final Dimension minSize)

Here you can find the source of restrictWindowMinimumSize(final Window wnd, final Dimension minSize)

Description

Adds listener to window that ensures that the window is not resized less then specified size.

License

Open Source License

Parameter

Parameter Description
wnd Window which size should be restricted.
minSize The minimum allowed size for the window.

Declaration

public static void restrictWindowMinimumSize(final Window wnd, final Dimension minSize) 

Method Source Code

//package com.java2s;
/*//from  www  . jav  a2s .c o m
 *   Swing Explorer. Tool for developers exploring Java/Swing-based application internals. 
 *     Copyright (C) 2012, Maxim Zakharenkov
 *
 *   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *   
 */

import java.awt.Component;

import java.awt.Dimension;

import java.awt.Window;
import java.awt.event.ComponentEvent;

import javax.swing.JInternalFrame;

public class Main {
    /**
     * Adds listener to window that ensures that
     * the window is not resized less then specified size.
     * In case if it is resized anyway it restores the
     * minimal size.
     *
     * @param wnd Window which size should be restricted.
     * @param minSize The minimum allowed size for the window.
     */
    public static void restrictWindowMinimumSize(final Window wnd, final Dimension minSize) {
        restrictMinimumSizeImpl(wnd, minSize);
    }

    /**
     * Adds listener to internal frame that ensures that
     * the frame is not resized less then specified size.
     * In case if it is resized anyway it restores the
     * minimal size.
     *
     * @param frame Frame which size should be restricted.
     * @param minSize The minimum allowed size for the window.
     */
    public static void restrictWindowMinimumSize(final JInternalFrame frame, final Dimension minSize) {
        restrictMinimumSizeImpl(frame, minSize);
    }

    private static void restrictMinimumSizeImpl(final Component component, final Dimension minSize) {
        // Ensure that window has normal size before resizing event
        Dimension curSize = component.getSize();
        if (curSize.width < minSize.width || curSize.height < minSize.height) {
            component.setSize(minSize);
        }
        // Adding listener. If added after size settings - will not be added if
        // one of arguments is NULL.
        component.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                Dimension curSize = component.getSize();
                if (curSize.width < minSize.width || curSize.height < minSize.height) {
                    Dimension newSize = new Dimension(Math.max(minSize.width, curSize.width),
                            Math.max(minSize.height, curSize.height));
                    component.setSize(newSize);
                }
            }
        });
    }
}

Related

  1. minSize(T comp, int width)
  2. paint(final Component c, Dimension size)
  3. preferredSize(JComponent component)
  4. resize(JComponent component, Dimension size)
  5. resizeComponentWidth(JComponent currentComponent, double maxComponentWidth)
  6. sameSize(JComponent[] components)
  7. saveComponentSize(JComponent comp, Preferences prefs, String name)
  8. setAllSizes(JComponent component, int width, int height)
  9. setComponentSize(int width, int height, JComponent l)