Question

I know there are bunch of similar topics, but I can't apply these solutions to my Project.

I've got a huge Java Application in which there are many time consuming operations on database. Everything is realized in a single thread so when the program is querying database the UI hangs. I got a task to make a loading window in described situations. I've tried JWindow objects, JFrame which is always on top, modal dialog, but none of them managed to work.

Here is the thing: I've got JDialog which contains JPanel. The panel consists of 2 input fields (login and password) and Log in button. When user press Logs in, there (on top) should appear "Loading..." window. Dialog is modal and is alwaysOnTop.

How should I do this?

Closest solution I get is:

JDialog modalDialog = new JDialog(dialog, "Busy", ModalityType.MODELESS);
modalDialog.add(new JLabel("Loading..."));
modalDialog.setSize(200, 150);
modalDialog.setLocationRelativeTo(dialog);
modalDialog.setVisible(true);

But the dialog doesn't draw itself well:

It contains elements from the background instead of Loading text...

After loading is done it shows Loading... text as desired.

When I add

      modalDialog.setUndecorated(true);

It doesn't draw on the first plane... and I don't want a bar with [X]

Was it helpful?

Solution

Don't put time-consuming operations on the same thread with Swing UI operations. All Swing UI operations take place on the "event dispatch thread", and it is up to you to keep time-consuming operations off of it.

Look at javax.swing.SwingWorker for a class that makes it easy to put time-consuming operations on a different thread.

Look at a javax.swing.JProgressBar for one that allows you to make a progress bar to display while a time-consuming operation is in process.

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