Java JDialog sizeDialog(JDialog dialog, int prefWidth, int prefHeight)

Here you can find the source of sizeDialog(JDialog dialog, int prefWidth, int prefHeight)

Description

Utility method to size a Dialog box correctly.

License

Apache License

Parameter

Parameter Description
dialog the dialog box to be sized
prefWidth - Pref width
prefHeight - Pref height

Declaration

public static void sizeDialog(JDialog dialog, int prefWidth, int prefHeight) 

Method Source Code

//package com.java2s;
/**/* w  w  w. java  2 s.c o  m*/
 *  Copyright 2009 Welocalize, Inc. 
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  
 *  You may obtain a copy of the License at 
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  
 */

import java.awt.Toolkit;
import java.awt.Dimension;

import javax.swing.JDialog;

public class Main {
    private static boolean checkedForNetscape = false;
    private static boolean bUsingNetscape;

    /**
       Utility method to size a Dialog box correctly.
       Routine will query dialog box for its preferred size.
       It will size dialog box based on
       - requeted size
       - preferred size
       - screen size
       @author Prakash
       @param dialog the dialog box to be sized
       @param prefWidth - Pref width
       @param prefHeight - Pref height
    */
    public static void sizeDialog(JDialog dialog, int prefWidth, int prefHeight) {//HF
        sizeDialog(dialog, prefWidth, prefHeight, false);
    }

    /**
       Utility method to size a Dialog box correctly.
       Routine will query dialog box for its preferred size.
       It will size dialog box based on
       - requeted size
       - preferred size
       - screen size
       @author Prakash
       @param dialog the dialog box to be sized
       @param prefWidth - Pref width
       @param prefHeight - Pref height
       @param shrinkOnly - Flag indicating app should not check dialog boxes preferred size
    */
    public static void sizeDialog(JDialog dialog, int prefWidth, int prefHeight, boolean shrinkOnly) {//HF
                                                                                                      //Log.println(Log.LEVEL3, "WindowUtil.sizeDialog: MinSize = " + prefWidth + ", " + prefHeight);
        Dimension prefSize = new Dimension(0, 0);
        if (isUsingNetscape() && !shrinkOnly) {
            // Get dialog box
            dialog.pack();
            prefSize = dialog.getPreferredSize();
            //Log.println(Log.LEVEL3, "PrefSize after pack: " + prefSize);
        }
        //
        // Now make compute new size (based on prefSize, prefSize)
        int w = Math.max(prefSize.width, prefWidth);
        int h = Math.max(prefSize.height, prefHeight);

        //Log.println(Log.LEVEL3, "Sizing dialog to: " + w + ", " + h);
        //
        // Note: This is a hack to adjust for Netscape's "Signed Applet: " line
        if (isUsingNetscape()) {
            //Log.println(Log.LEVEL3, "Netscape: Increasing height by : " + dialog.getInsets().bottom);
            h += dialog.getInsets().bottom;
        }
        //
        // Make sure we are note larger than screensize
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        w = Math.min(w, screenSize.width - 20);
        h = Math.min(h, screenSize.height - 20);
        //
        // We have a size.
        //Log.println(Log.LEVEL3, "WindowUtil.sizeDialog: Setting size to: " + w + ", " + h);
        dialog.setSize(w, h);
    }

    /**
       Utility method to to check if applet is running in Netscape.
       This is needed because of Netscape's security stuff.
       Method checks if class netscape.security.PrivilegeManager is found.
       @author Prakash
    */
    public static boolean isUsingNetscape() {
        if (!checkedForNetscape) {
            checkedForNetscape = true;
            String jvmVendor = System.getProperty("java.vendor");
            //Log.println(Log.LEVEL2, "jvmVendor = " + jvmVendor);
            jvmVendor = jvmVendor.toLowerCase();
            if (jvmVendor.indexOf("netscape") >= 0) {
                bUsingNetscape = true;
            } else {
                bUsingNetscape = false;
            }
        }
        //Log.println(Log.LEVEL3, "isUsingNetscape: " + bUsingNetscape);
        return bUsingNetscape;
    }
}

Related

  1. setCursorFree(JDialog dialog)
  2. setDirty(JDialog dialog, boolean isDirty)
  3. setHelpDialogLoc(JButton odsHelpButton, JDialog helpDialog)
  4. setSize(JDialog dialog, int width, int height)
  5. setWinVisible(final JDialog win, final boolean vis)
  6. waitForInput(JDialog view)