Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I put an hourglass cursor on a JCombobox ?

Posted on 2004-04-13
16
Medium Priority
?
571 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

688 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