java swing popup.show() doesnt work, help

Hi i am very new to swing. and for the life of me, i cannot get this popup window to show up properly after the mouseclick() is called, i can confirm mouseclick event is definitely clicked by the system.out that got printed.  Here's what happened:

1) after mouse click, the popup window will appear but only half loaded, basically a white window with all the text in the middle missing

2) the popup only finish loading and show properly AFTER the entire mouseClicked() function finishes.

question:  how do i get the popup window to show up and paint properly as soon as popup.show() is called. I am using it as "loading please wait" window, but right now it just shows a white blank window until everything finishes processing. I want it to draw/paint properly as soon as popup.show() is called.
thanks
public class someListener extends MouseAdapter
{
  private Popup popup;
  private JLabel sortingProgress;
   public someListener ()
   {
             sortingProgress = new JLabel("Sorting...");
             popup = PopupFactory.getSharedInstance().getPopup(null, sortingProgress, 200, 200);
     }
 
      public void mouseClicked(MouseEvent e) 
       {
                System.out.println("mouse clicked");
                  popup.show();
                 try{Thread.sleep(1000000);}catch(Exception ee){} //or do some processing
        }
}

Open in new window

LVL 1
gagaliyaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
JPopupMenu popup = new JPopupMenu();
// add actions to your popup here
sortingProgress.setComponentPopupMenu(popup);

0
Thomas4019Commented:
Personally, i avoid mouseClicked. I either just use mousePressed, or mouseReleased.

Yes, your popup cannot paint while the mouseClicked is running. When a mouse clicks, the Java JRE calls your mouseClicked method in the Event Thread. Only one method can be running per Thread. Events are only processed with the Event Thread. So if you stop that Thread. You pause all AWT/Swing events period. If you block that Thread, you application likely gets recognized by windows as "unresonsive" as it cannot respond to events.

So in conclusion, do not do long processing in the AWT Event Thread.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Thomas4019Commented:
To get it to show immediately, you would have to write you own JFrame or JDialog designed to paint without the Event Thread or just dont "block" the Event Thread.
0
Mick BarryJava DeveloperCommented:
For long processing have a look at using the SwingWorker class

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/simple.html

0
CEHJCommented:
Sounds like you need to implement a 'splash screen'

http://java.sun.com/docs/books/tutorial/uiswing/misc/splashscreen.html
0
Mick BarryJava DeveloperCommented:
0
gagaliyaAuthor Commented:
Hi guys, i tried following code it worked perfectly on first click, but on 2nd click and onwards nothing happens. From my understanding reading the links, basically the mouseClicked event is running, so the draw has to wait as it's single threaded.  But i thought by changing this to mousePressed and mouseReleased, it would solve the problem as aftrer mousePressed, the thread finishes processing, so the poup should be drawn, then mouseReleased kicks in.

It works fine the first click, but then nothing happens on all subsequent clicks, why?

 
      public void mousePressed(MouseEvent e)
       {                  popup.show(); }

      public void mouseReleased(MouseEvent e)
     {           try{Thread.sleep(1000000);}catch(Exception ee){} //or do some processing
                popup.hide();
       }
0
Mick BarryJava DeveloperCommented:
because the event dispatch thread is  a sleep(), so it cannot respond to the 2nd press

0
Thomas4019Commented:
You still blocking the Thread with the mouseReleased().

Sequence of events in Event Thread
1. call mousePressed();
2. paint popup (maybe if it can fit it in time)
3. call mouseReleased.
4. Threads asleep so nothing else happens
0
gagaliyaAuthor Commented:
thank you all!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.