Link to home
Start Free TrialLog in
Avatar of gagaliya
gagaliya

asked on

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

Avatar of Mick Barry
Mick Barry
Flag of Australia image

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

ASKER CERTIFIED SOLUTION
Avatar of Thomas4019
Thomas4019
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
For long processing have a look at using the SwingWorker class

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gagaliya
gagaliya

ASKER

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();
       }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you all!