Java JFrame calculateMaxMenuItems(JFrame window)

Here you can find the source of calculateMaxMenuItems(JFrame window)

Description

calculate Max Menu Items

License

Apache License

Declaration

public static int calculateMaxMenuItems(JFrame window) 

Method Source Code

//package com.java2s;
/*//  w  w w .  ja v a 2  s.  c om
 * WbSwingUtilities.java
 *
 * This file is part of SQL Workbench/J, http://www.sql-workbench.net
 *
 * Copyright 2002-2017, Thomas Kellerer
 *
 * Licensed under a modified Apache License, Version 2.0
 * that restricts the use for certain governments.
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at.
 *
 *     http://sql-workbench.net/manual/license.html
 *
 * 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.
 *
 * To contact the author please send an email to: support@sql-workbench.net
 *
 */

import java.awt.Dimension;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GraphicsConfiguration;

import java.awt.GraphicsEnvironment;

import java.awt.Insets;

import java.awt.Toolkit;

import javax.swing.JFrame;

import javax.swing.UIDefaults;
import javax.swing.UIManager;

public class Main {
    public static int calculateMaxMenuItems(JFrame window) {
        UIDefaults def = UIManager.getDefaults();
        Font itemFont = def.getFont("MenuItem.font");
        Font barFont = def.getFont("MenuBar.font");

        int itemHeight = 16;
        int barHeight = 24;

        FontMetrics fm = itemFont == null ? null : window
                .getFontMetrics(itemFont);
        if (fm != null) {
            itemHeight = fm.getHeight();
        } else if (itemFont != null) {
            itemHeight = convertPointSizeToPixel(itemFont.getSize());
        }
        FontMetrics barMetrics = barFont == null ? null : window
                .getFontMetrics(barFont);
        if (barMetrics != null) {
            barHeight = fm.getHeight();
        } else if (barFont != null) {
            barHeight = convertPointSizeToPixel(barFont.getSize());
        }

        Insets itemInsets = def.getInsets("MenuItem.margin");
        if (itemInsets != null) {
            itemHeight += itemInsets.top + itemInsets.bottom;
        }

        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int screenHeight = (int) screen.getHeight();
        int usableHeight = screenHeight - barHeight * 3;
        int numItems = usableHeight / itemHeight;
        return numItems;
    }

    public static int convertPointSizeToPixel(int points) {
        int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
        int pixel = points * dpi / 72;
        return pixel;
    }

    public static Dimension getScreenSize() {
        return getScreenSize(getScreenOffset());
    }

    public static Dimension getScreenSize(Insets insets) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        screen.width -= (insets.left + insets.right);
        screen.height -= (insets.bottom + insets.top);
        return screen;
    }

    public static Insets getScreenOffset() {
        GraphicsEnvironment ge = GraphicsEnvironment
                .getLocalGraphicsEnvironment();
        GraphicsConfiguration conf = ge.getDefaultScreenDevice()
                .getDefaultConfiguration();

        Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(conf);
        return insets;
    }
}

Related

  1. btnMnemonicAndToolTip(JFrame frame, JButton a, JButton b, JButton c, JButton e, JButton h, JButton i, JButton l, JButton m, JButton o, JButton p, JButton s, JButton t, JButton enter, JButton altEnter)
  2. buildButton(String btnName, JFrame frame, int x, int y, int w, int h)
  3. buildLabel(JFrame frame, String txt, int x, int y, int w, int h)
  4. buildTxtField(JFrame frame, int x, int y, int w, int h)
  5. calcLocation(JFrame main, JDialog dialog)
  6. calculateMaxMenuItems(JFrame window)
  7. centre(JFrame frame)
  8. centreJustifyFrame(final JFrame frame, final int y_position)
  9. centreWindow(Window c, JFrame frame)