Java JFrame Size restrictWindowMinimumSize(final JInternalFrame frame, final Dimension minSize)

Here you can find the source of restrictWindowMinimumSize(final JInternalFrame frame, final Dimension minSize)

Description

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

License

Open Source License

Parameter

Parameter Description
frame Frame which size should be restricted.
minSize The minimum allowed size for the window.

Declaration

public static void restrictWindowMinimumSize(final JInternalFrame frame, final Dimension minSize) 

Method Source Code

//package com.java2s;
/*//from  w  ww  . j ava 2  s  .  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. isNormalSizeMode(JFrame window)
  2. locateWindow(JFrame parent, Dimension mySize, Dimension offset, JDialog me)
  3. lockInMinSize(final JFrame frame)
  4. positionFrame(final JFrame frame, final Dimension viewSize, final int addX, final int addY)
  5. resizeApp(JFrame app)
  6. setMinMaxSizeFrame(JFrame frame)
  7. setMinSizeFrame(JFrame frame)
  8. setSizeBasedOnResolution(final JFrame frame)
  9. setSizeWithinScreen(JFrame frame, int preferredWidth, int preferredHeight)