Java JComponent Size getWindowSize(JComponent start)

Here you can find the source of getWindowSize(JComponent start)

Description

return the size of the window

License

Apache License

Parameter

Parameter Description
start enclosed component

Declaration

public static Dimension getWindowSize(JComponent start) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.swing.*;

import java.awt.*;

public class Main {
    /**/*w  w w.j  a v  a 2  s .c  o  m*/
     * return the size of the window
     *
     * @param start enclosed component
     * @return
     */
    public static Dimension getWindowSize(JComponent start) {
        return getTopAncestor(start).getSize();
    }

    public static Component getTopAncestor(Component c) {
        Component ret = c.getParent();
        if (ret == null)
            return c;
        return getTopAncestor(ret);
    }

    /**
     * search of the hierarcy of components for an instance of a given class
     *
     * @param start    - starting component
     * @param theClass - required class
     * @return possibly null component - null says no ancestor found
     */
    public static Container getTopAncestor(JComponent start) {
        Container parent = start.getParent();
        if (parent == null)
            return start;
        if (parent instanceof JComponent)
            return getTopAncestor((JComponent) parent);
        if (parent instanceof JFrame)
            return parent;
        if (parent instanceof JDialog)
            return parent;
        if (parent instanceof JWindow)
            return parent;
        else
            return null;
    }
}

Related

  1. getSize(byte[] data)
  2. getSize(Component c, int sizeflag)
  3. getValidatedSize(Component c, int sizeflag)
  4. getViewableScreenSize()
  5. getVisibleSizeInViewport(Component c)
  6. inset(Component c, int insetSizeHor, int insetSizeVert)
  7. isSmallSizeVariant(JComponent c)
  8. makeEqualSize(JComponent reference, JComponent... others)
  9. makeSameSize(Component... components)