Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I put an hourglass cursor on a JCombobox ?

Posted on 2004-04-13
16
Medium Priority
?
578 Views
Last Modified: 2008-03-10
My Code looks like this:
        .....
        JComboBox  phaseBox = new JComboBox(items);
      phaseBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                  phaseBox.setCursor(new Cursor(Cursor.WAIT_CURSOR ));
                        handleAction("select");
                  phaseBox.setCursor(Cursor.getDefaultCursor());
            }
      });
        .....

The problem is:
The "action" takes about 4 seconds. During this time, the option in the combobox stays selected, but the wait cursor is never shown. It looks like the application freezes. How do I make this cursor visible upon selecting an option from the combobox.

tia,

Arne.
0
Comment
Question by:ArneDK
  • 6
  • 6
  • 4
16 Comments
 
LVL 13

Expert Comment

by:Webstorm
ID: 10811846
Hi ArneDK,

Try this :
    phaseBox.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

If it doesn't work, try to change the window/frame/dialog cursor instead :

0
 
LVL 37

Expert Comment

by:zzynx
ID: 10811861
You better set the cursor of the Frame your items are in, not on your items (combobox) themselves.

yourFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
0
 

Author Comment

by:ArneDK
ID: 10812052
Thanks fot the fast reactions.
I've tested both propositions, but that doesn't seem to help.
I've changed the cursor onthe parent of this combobox and on several other object including the main frame, but nothing happens.
Other elements in my application have no problem with this (JButtons and JTree elements).
So it is a specific JComboBox related problem.
While the action is being handled, the combobox displays the list with the selected item highlighted.
I think that i should remove the focus off the jcombobox after selecting an item and before handling the action, but i can't find how to do that.

Arne.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 37

Expert Comment

by:zzynx
ID: 10812272
Try to replace

           handleAction("select");

by just sleeping:

           try { Thread.sleep(5000); } catch (java.lang.InterruptedException ex) { }

and see if the cursor appears.
(to exclude it is your handleAction() function that is responsible for this unexpected behaviour)
0
 

Author Comment

by:ArneDK
ID: 10812476
The problem persists.
I noticed now that for a few milliseconds, the cursor seems to change and change back.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10812714
>> I noticed now that for a few milliseconds, the cursor seems to change and change back.
That means it is working, no?

How does that handleAction("select") looks like?
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10812850
If handleAction create some thread doing the action, you must restore the cursor after the action is finished :

      phaseBox.setCursor(Cursor.getDefaultCursor()); // last statement of the Thread's run() method

0
 

Author Comment

by:ArneDK
ID: 10812973
What it does now is this:
I select an option from the combobox.
A Jtree in another panel is being refreshed.
Since this includes a database query, it takes about 4 seconds to refresh the jtree.
During this time, I would like to display an hourglass.
But it doesnt how, except very quickly after the refresh (after the handleAction("select") ) during a few milliseconds.
So the handleAction function performs a DB query and builds a jtree out of these results.
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 300 total points
ID: 10813210
Try to make a new Thread for handling the action :

               phaseBox.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR ));
               (new Thread(){
                     public void run(){
                            handleAction("select");
                            phaseBox.setCursor(Cursor.getDefaultCursor());
                     }
                }).start();
   
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10813285
Webstorm, if he doesn't touch/change the cursor in handleAction()
then, is there any need to create a Thread and perform

           phaseBox.setCursor(Cursor.getDefaultCursor());
?

Arne, are you sure not to touch the cursor in your handleAction()
[ and subfunctions like querying the Db and refreshing the tree ]?
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10813337
>> Webstorm, if he doesn't touch/change the cursor in handleAction() then, is there any need to create a Thread and perform ...

Because the java thread will not update the cursor until handleAction terminates (thread scheduling). If you create a new thread to call handleAction, the java thread (not the same) will update the cursor.

0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10813364
>>  phaseBox.setCursor(Cursor.getDefaultCursor());
to restore the default cursor after the handleAction() returns.

0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 75 total points
ID: 10813414
>> Because the java thread will not update the cursor until handleAction terminates (thread scheduling).
I think that's just the goal of the author.
Since he writes:

             phaseBox.setCursor(new Cursor(Cursor.WAIT_CURSOR ));  // Cursor set to hourglass
             handleAction("select");                                                     // cursor isn't touched as long as this takes
             phaseBox.setCursor(Cursor.getDefaultCursor());                 // cursor changed back to normal
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10813886
So? You create a new thread now?
Does that mean that you were changing the cursor in handleAction()?

Thanks for accepting.
That makes us answering your future questions too.
0
 

Author Comment

by:ArneDK
ID: 10813896
The thread thing worked out fine !
This is my final working code:

               phaseBox.addActionListener(new java.awt.event.ActionListener() {
                  public void actionPerformed(java.awt.event.ActionEvent evt) {
                        getParent().setCursor(new Cursor(Cursor.WAIT_CURSOR));
                        (new Thread(){
                              public void run(){
                                    handleAction("Select");
                                    getParent().setCursor(Cursor.getDefaultCursor());
                              }
                        }).start();
                  }
            });

thx everyone, especially zzynx and Webstorm.

Arne.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10814073
:-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

876 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