Solved

setting waiit cursor after calling a jdialog doesn't work

Posted on 2010-11-18
9
449 Views
Last Modified: 2012-05-10
Hi, I have below code.

MyUpload mu = new MyUpload((Frame)this.getParent(), true, mc.getEmp(), mc.getCid(), getCodeBase());
if(mu.result.equals("Completed")){
    getMRec();
}

and MyUpload.java looks like this:
public MyUpload(java.awt.Frame parent, boolean modal, String empNum, String contID, URL codeBase) {
    super(parent, modal);
    ...
}

MyUpload is a JDialog where some file upload gets done. Once the upload is done the result is stored in mu.result, then it runs getMRec() function from the main frame.

getMRec() has following simple logic that set the cursor "busy" while it's doing some job:
void getMRec(){
    getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    doSomething();
    getRootPane().setCursor(null);
}

After uploading is done from the dialog and the dialog is closed, and doSomething() from getMRec() has executed fine but the cursor never changed to a busy cursor. If I comment out the dialog part then the cursor changes to a busy cursor while doSomething() is running. Any idea why?
0
Comment
Question by:shepp_it
  • 5
  • 4
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 34169466
Swing is single thread so you need to switch the cursor from the event dispatch thred
and do your processing in a seperate thread

http://helpdesk.objects.com.au/java/how-do-i-update-my-gui-from-any-thread

you may also find the swing worker useful

http://download.oracle.com/javase/tutorial/uiswing/concurrency/worker.html
0
 

Author Comment

by:shepp_it
ID: 34174466
getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
doProcess();
getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

Above works fine.
It just won't work if I have a code that pops up JDialog as below;

MyDialog md = new MyDialog((Frame)this.getParent(), true);
getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
doProcess();
getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

If I use JOptionPane instead of JDialog then it works fine:

Object objStr = JOptionPane.showInputDialog(getRootPane(), "Please choose a payroll period", "Payroll",
            JOptionPane.QUESTION_MESSAGE, null, payroll, null);
getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
doProcess();
getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

I'm using it on applet. it could be why?
0
 
LVL 92

Expert Comment

by:objects
ID: 34177208
The JDialog is a top level window, you need to set the cursor on it
0
 

Author Comment

by:shepp_it
ID: 34177231
I want to set cursor after I close the jdialog.

//pop up a dialog
MyDialog md = new MyDialog((Frame)this.getParent(), true);

//once finished with dialog, change the cursor
getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 34177296
then use a WindowListener to change the cursor
0
 
LVL 92

Expert Comment

by:objects
ID: 34177321
0
 

Author Comment

by:shepp_it
ID: 34183954
Man you're awesome!!!!!!!! I don't know why I never thought about a WindowListener!!
Thank you!
0
 

Author Comment

by:shepp_it
ID: 34183980
For those who's having the same problem, in above example it's using windowClosing but I used windowClosed event instead to get it working in my case. My code:
My_Upload mu = new My_Upload((Frame)this.getParent(), true, mc.getEmp(), mc.getCid(), getCodeBase());

mu.setLocationRelativeTo(this);

mu.setVisible(true);

mu.addWindowListener(new WindowAdapter(){

    public void windowClosed(WindowEvent we){

        getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

    }

});

        

if(mu.getResult().equals("Completed")){

    mu.dispose();

    getMyRec();

}



...



private void getMyRec(){

    try {

        if(getRootPane().getCursor()!=Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)){

            getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

        }

        

        // do something here...

    }finally{

        getRootPane().setCursor(Cursor.getDefaultCursor());

    }

}

Open in new window

0
 

Author Comment

by:shepp_it
ID: 34184006
I'm sorry, actually I have to use windowClosing event, not windowClosed. Using windowClosed will cause the cursor never going back to the default cursor... !
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now