Solved

How do I put an hourglass cursor on a JCombobox ?

Posted on 2004-04-13
16
523 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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 100 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 25 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 53
Java SE 8u111  Lot of stuff broke 11 69
Java string replace 11 47
difference between sorce folder and folder in eclipise 3 13
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

777 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