Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

Call setAutoRequestFocus(false) for your JDialog.

Edit - additional info + rant: We all hate it when are are typing and some background program or our operation systems decides to throw a window in our face, snatching our keyboard input, possibly even clicking something we didn't even get a chance to see. I think that not giving focus to popups should be the default behavior. With this said, if in addition to the above method call you call setAlwaysonTop(true) or override its setVisible method such that you bring it forward on your screen without giving it focus, it will come in front of other applications, but not gain focus. This way you can get your notification without ruining your day. I suggest it just in case you are annoyed only by the focus steal and not by the dialog actually showing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top