Search code examples
javaswingjdialog

JDialog pops up on top of all windows


I'm creating a JDialog as information popup appearing on top of my main window (JFrame)when some conditions are met. But when i'm working in another window on top of my application and the JDialog popup appears, it pops up at the top of the other application. I would expect the JDialog popup to appear at the top of my application window, but not on top of all applications.

How can i do this?

Status quo:

_____________     ____________________________     ____________
| mainframe |  -> | other application window*|  -> | my popup |
-------------      ---------------------------      -----------

Expected:

_____________     _____________      __________________________
| mainframe |  -> | my popup  |  -> | other application window*|  
-------------      ------------     ----------------------------      

*arbitrary other application like Word, Outlook, Counter Strike, whatever :)

The used code:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class DialogSSCCE extends JFrame {

    private static final long   serialVersionUID    = 1L;
    private JPanel  contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    DialogSSCCE frame = new DialogSSCCE();
                    frame.setAutoRequestFocus(false);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public DialogSSCCE() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JButton btnTest = new JButton("test");
        btnTest.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                javax.swing.Timer timerDialog = new javax.swing.Timer(3000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JDialog dia =  new JDialog(DialogSSCCE.this);
                        dia.setUndecorated(true);
                        dia.setModal(false);
                        dia.setBounds(100, 100, 200, 200);
                        dia.setAutoRequestFocus(false);
                        dia.setAlwaysOnTop(false);
                        dia.setUndecorated(true);
                        dia.setVisible(true);
                    }
                });
                timerDialog.setRepeats(true);
                timerDialog.start();
            }
        });
        contentPane.add(btnTest, BorderLayout.CENTER);
    }

}

Solution: I initialized the JDialog with its parent frame, but the parent frame wasn't ready so the dialog was inititalized with null. That explained why the dialog's behaved indepent from the main frame


Solution

  • I accidently inititalized the JDialog with null instead of its owner JFrame, because the main frame wasn't ready initialized when creating the JDialog