show JFrame - Java Swing

Java examples for Swing:JFrame

Description

show JFrame

Demo Code


import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Window;
import java.io.IOException;
import java.util.prefs.Preferences;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main{
    public static JFrame showFrame(String title, JComponent comp) {
        final JFrame dialog = new JFrame(title);
        try {/*from  w w  w .  jav a  2  s  .  c o  m*/
            dialog.setIconImage(ImageIO.read(DialogUtils.class
                    .getResource("/mediamatrix/resources/Icon.png")));
        } catch (IOException ignored) {
        }

        dialog.getContentPane().setLayout(new BorderLayout());
        dialog.getContentPane().add(comp, BorderLayout.CENTER);
        dialog.pack();
        final Rectangle maxRectangle = GraphicsEnvironment
                .getLocalGraphicsEnvironment().getMaximumWindowBounds();
        int x = 0;
        int y = 0;
        int w = dialog.getWidth();
        int h = dialog.getHeight();
        if (w + x > maxRectangle.width) {
            x = 0;
            w = maxRectangle.width;
        }
        if (h < 400) {
            h = 400;
        }
        if (w < 400) {
            w = 400;
        }
        if (h + y > maxRectangle.height) {
            y = 0;
            h = maxRectangle.height;
        }
        if (x < 0) {
            x = 0;
        }
        if (System.getProperty("os.name").indexOf("Mac") >= 0 && y < 20) {
            y = 20;
        }
        dialog.setBounds(x, y, w, h);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        return dialog;
    }
}

Related Tutorials