Java JComponent Size getOptimalScreenSize(Container container, Dimension currentDim)

Here you can find the source of getOptimalScreenSize(Container container, Dimension currentDim)

Description

Calculates the optimal screen size for the given container.

License

Open Source License

Parameter

Parameter Description
container the container whose optimal subcomponents size shall be calculated
currentDim the current dimension to start with, any smaller dimension is ignored. Only if a subcomponent needs more space than given here, the dimension is adapted

Return

The optimal screen size for the given container

Declaration

public static Dimension getOptimalScreenSize(Container container, Dimension currentDim) 

Method Source Code


//package com.java2s;
/* /*  ww w  . j a  v a2  s. co m*/
 * Copyright (C) 2014 Institute for Bioinformatics and Systems Biology, University Giessen, Germany
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JScrollPane;

public class Main {
    /**
     * Calculates the optimal screen size for the given container. Optimal size
     * means, that all subcomponents can be displayed in their full size. Starts
     * the calculation with the given <code>currentDim</code>.
     * @param container the container whose optimal subcomponents size shall be 
     * calculated
     * @param currentDim the current dimension to start with, any smaller 
     * dimension is ignored. Only if a subcomponent needs more space than given
     * here, the dimension is adapted
     * @return The optimal screen size for the given container
     */
    public static Dimension getOptimalScreenSize(Container container, Dimension currentDim) {
        Component[] comps = container.getComponents();
        for (int i = 0; i < comps.length; ++i) {
            try {
                currentDim = getOptimalScreenSize((Container) comps[i], currentDim);
                Component comp = comps[i];
                int width = comp.getWidth();
                int height = comp.getHeight();
                if (comp instanceof JScrollPane) {
                    JScrollPane pane = (JScrollPane) comp;
                    Dimension scrollViewDim = pane.getViewport().getViewSize();
                    int totalHeight = scrollViewDim.height + comp.getLocationOnScreen().y;
                    if (currentDim.height < totalHeight) {
                        currentDim.height = totalHeight;
                    }
                    if (currentDim.width < scrollViewDim.width) {
                        currentDim.width = scrollViewDim.width + comp.getLocationOnScreen().x;
                    }
                }
                if (currentDim.height < height) {
                    currentDim.height = height + comp.getLocationOnScreen().y;
                }
                if (currentDim.width < width) {
                    currentDim.width = width + comp.getLocationOnScreen().x;
                }
            } catch (IllegalStateException e) {
                //nothing to do: ignoring non visible components of the current container
            }
        }
        return currentDim;
    }
}

Related

  1. freezeSize(JComponent c, Dimension s)
  2. getCentralLocation(Component sourceComp, Component comp, Dimension size)
  3. getComponentOfSameSize(final JComponent c)
  4. getLocalScreenSize(Component invoker)
  5. getMaxSize(JComponent[] components)
  6. getPreferredSize(JComponent component, int width)
  7. getPreferredSize(String html, boolean width, int prefSize)
  8. getPreferredSize(Window window)
  9. getPreferredSizeComponent(JComponent component)