Solved

How do I put an hourglass cursor on a JCombobox ?

Posted on 2004-04-13
16
502 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 13

Accepted Solution

by:
Webstorm earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
>>  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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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

8 Experts available now in Live!

Get 1:1 Help Now