Java JFrame Parent getRootFrame()

Here you can find the source of getRootFrame()

Description

get Root Frame

License

Apache License

Declaration

public static Frame getRootFrame() 

Method Source Code

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

import javax.swing.*;

import java.awt.*;

public class Main {
    private static Frame frame;

    public static Frame getRootFrame() {
        if (frame == null) {
            frame = new Frame();
            frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
            positionCenterScreen(frame);
        }//from   w w w.  j a  v a  2  s .c  o  m
        return frame;
    }

    public static void positionCenterScreen(Window window) {
        Dimension maxDimension = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension wSize = window.getSize();
        int maxWidth = maxDimension.width;
        int maxHeight = maxDimension.height;
        // fit on window
        if (wSize.height > maxHeight) {
            wSize.height = maxHeight;
        }
        if (wSize.width > maxWidth) {
            wSize.width = maxWidth;
        }
        window.setSize(wSize);
        if (maxDimension.width != wSize.width && maxDimension.height != wSize.height) {
            int x = (maxDimension.width - wSize.width) / 2;
            int y = (maxDimension.height - wSize.height) / 2;
            window.setLocation(x, y);
        } else {
            if (window instanceof JFrame) {
                JFrame f = (JFrame) window;
                f.setExtendedState(JFrame.MAXIMIZED_BOTH);
            } else {
                window.setLocation(0, 0);
            }
        }
        window.toFront();
    }
}

Related

  1. getParentFrame(JComponent comp)
  2. getParentFrame(JComponent comp)
  3. getParentFrameForCompomponent(Component comp)
  4. getParentInternalFrame(Component comp)
  5. getRealFrameParent(Component c)
  6. getRootFrame(Component aComp)
  7. getRootFrame(Component c)
  8. getRootFrame(java.awt.Component c)
  9. getRootFrame(JComponent component)