JDialog dialog = new JDialog(parent JFrame, "blabla");
dialog.setLayout(new BorderLayout());
JLabel label = new JLabel("more blabla");
dialog.getContentPane().add(label, BorderLayout.CENTER);
dialog.setSize(new Dimension(280, 80));
dialog.setLocationRelativeTo(parent JFrame);
dialog.setVisible(true);
//part of code that takes time to execute
//actually, I'm sending an email here, but it doesn't really matter what I do,
//as you will read below
dialog.dispose();
I have the code above and it's use is to display a message to the user while some code is executing (execution of code takes about 5-10 seconds, I don't want user to think the program froze).
The problem is, JLabel that was added to the dialog doesn't appear at all. Whatever I add to the dialog, it doesn't appear, to be exact.
However, if I set JDialog to modal (add true as the last argument to the above constructor), it would appear, but then the code I wanted to be executed wouldn't run until the dialog was closed, defying it's purpose.
The code that would be executing doesn't really matter due to it's position in the code, and I tried leaving it as it's currently above - with no executing code at all - the dialog would show up for split second, but I could saw it was empty.
I know I could create a conformation window and say "standby for 10 seconds, code is executing", but I don't want to do it that way.
I also tried swaping JDialog for JFrame, text still wouldn't appear.
What am I missing?
If the long running task is executed on
Event Dispatch Thread
(EDT) then the dialog may not have a chance to paint/repaint itself until the task is finished. Check out Concurrency in Swing tutorial for more details on EDT.Here is a sample that utilizes your code and it demonstrates the misbehaved dialog :
Consider refactoring your implementation by executing the long running task on a worker thread. For example you can accomplish that with use of SwingWorker.
Here is a demo that illustrates the same dialog with a simple worker: