-2

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]

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Damian
  • 13
  • 1
  • 6
  • try UpdateUI and rePaint. – Koray Tugay Jan 14 '14 at 12:40
  • Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Jan 14 '14 at 12:59
  • *"I know there are bunch of similar topics, but I can't apply these solutions to my Project."* Why not? Link to the top 5. – Andrew Thompson Jan 14 '14 at 13:03
  • http://stackoverflow.com/questions/4870273/java-swing-modal-loading-screen http://stackoverflow.com/questions/21113768/swing-based-loading-frame-hangs-on-db-query http://stackoverflow.com/questions/12937759/displaying-a-loading-jframe-while-a-loop-is-running-in-main-thread – Damian Jan 14 '14 at 14:39
  • I've tried but it turns out that problem here is I can't have two frames with setAlwaysOnTop(true) - and I spend almost 3 days figuring it out :) And I still don't know why code I pasted did not work. – Damian Jan 14 '14 at 14:41

1 Answers1

2

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.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • yea, I know but like I said this is not my application :) – Damian Jan 14 '14 at 13:01
  • Actually, you said "I've got an application..." and did not say it was not 'yours'. This is still the way to solve the problem. – arcy Jan 14 '14 at 13:03
  • ok.. I figured out that the described problem appears when there are two frames with setAlwaysOnTop(true) invoked. So removing one invocation of this method solved the problem for me. I wonder if I got -2 points for bad English or for a stupid question :) – Damian Jan 14 '14 at 13:04
  • I expect it was neither of those, but the vagueness of the question. Those of us here attempting to help you don't really have enough information, and this is confirmed by the fact that my guess at the problem wasn't really the problem. After a while we stop answering such questions altogether, because it often turns out to be a waste of time. – arcy Jan 14 '14 at 13:13
  • Ok, I promise to be more exact in the future. Thanks – Damian Jan 14 '14 at 13:15
  • I added one for *"I know there are bunch of similar topics, but I can't apply these solutions to my Project."* because you did not link to them and explain why they did not work for you. But mostly when it was quite obvious a) You had learned nothing from them, and b) You were now trying things at random, in hopes of fixing the code. 'Coding by magic'. -- Programming does not work by magic. *"bad English"* ..was not a factor in my down-vote. – Andrew Thompson Jan 14 '14 at 13:38