show JDialog - Java Swing

Java examples for Swing:JOptionPane

Description

show JDialog

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 void showDialog(String title, JComponent comp,
            Window window) {/*from ww  w.  ja  v  a  2  s  . c om*/
        final JDialog dialog = new JDialog(window, ModalityType.MODELESS);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setTitle(title);
        try {
            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 > 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);
    }
    public static void showDialog(String title, JComponent comp,
            JComponent parent) {
        showDialog(title, comp, SwingUtilities.getWindowAncestor(parent));
    }
}

Related Tutorials