Solved

How do I put an hourglass cursor on a JCombobox ?

Posted on 2004-04-13
16
555 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

627 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