add Modeless JDialog Window - Java Swing

Java examples for Swing:JDialog

Description

add Modeless JDialog Window

Demo Code


//package com.java2s;
import java.awt.BorderLayout;
import java.awt.Component;

import java.awt.Dialog.ModalityType;

import java.awt.Frame;

import java.awt.Window;

import javax.swing.JDialog;

public class Main {
    static public JDialog addModelessWindow(Component jpanel, String title) {
        return addModelessWindow((Window) null, jpanel, title);
    }//from  ww w. j av  a 2s .  c o m

    static public JDialog addModelessWindow(Window mainWindow,
            Component jpanel, String title) {
        JDialog dialog;
        if (mainWindow != null) {
            dialog = new JDialog(mainWindow, title);
        } else {
            dialog = new JDialog();
            dialog.setTitle(title);
        }
        dialog.getContentPane().setLayout(new BorderLayout());
        dialog.getContentPane().add(jpanel, BorderLayout.CENTER);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.pack();
        dialog.setLocationRelativeTo(mainWindow);
        dialog.setModalityType(ModalityType.MODELESS);
        dialog.setSize(jpanel.getPreferredSize());
        dialog.setVisible(true);
        return dialog;
    }

    static public JDialog addModelessWindow(Frame mainWindow,
            Component jpanel, String title) {
        JDialog dialog = new JDialog(mainWindow, title, true);
        dialog.getContentPane().setLayout(new BorderLayout());
        dialog.getContentPane().add(jpanel, BorderLayout.CENTER);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.pack();
        dialog.setLocationRelativeTo(mainWindow);
        dialog.setModalityType(ModalityType.MODELESS);
        dialog.setSize(jpanel.getPreferredSize());
        dialog.setVisible(true);
        return dialog;
    }
}

Related Tutorials