Here you can find the source of getOptimalScreenSize(Container container, Dimension currentDim)
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 |
public static Dimension getOptimalScreenSize(Container container, Dimension currentDim)
//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; } }