Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Changing mouse cursor in JApplet on click

Posted on 1998-07-06
5
Medium Priority
?
310 Views
Last Modified: 2013-11-23
Hi everyone,

I have a JApplet which submits sql queries via JDBC to a database.
When the user clicks a certain button a query is initiated.
Since I would like to notify my user that he should wait I'd like to change the
mouse cursor to an hour glass cursor and change it back to normal when the query
is finished.
Changing the cursor in JApplet doesn't seem to work due to some bug that should be
fixed in the next version of the Java JFC.
In the mean while I'd like to change the jbutton's color, text or anything that might indicate that
the user should wait.
The problem is that due to some repaint problems the change of background color or any other
visual property does not occur.

I would most appriciate if anyone knows and could share it with me, how I may change background color when
a jbutton was pressed and change it back after a certain process had ended.

Thanks in advance,
Guy Loewy.
0
Comment
Question by:optimal
[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
  • 3
5 Comments
 

Author Comment

by:optimal
ID: 1224838
Edited text of question
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1224839
Changing the buttons background is done by button.setBackground(Color.xxx). I am not aware of any other way of doing that. Is that not working?  I guess you should put this piece of code exactly where you were planning to put the code changing the cursor. For example in button's action listener you may change the button's background, then call some method which does processing and then change the background back after that method returns. Or you can pass the reference to the button in that method which does not seem very clean.

Rather than changing the background color why don't you create a status line at the bottom of the screen and update it as something happens. It may be as simple as a JLabel component positioned at the bottom of the main container. Or you may bring upa dialog with a progress bar whic is a bit more involved.

Let me know if I answered your question.
0
 

Author Comment

by:optimal
ID: 1224840
Thanks msmolyak,

But, all you mensioned is exactlly what I'm trying to do without solving my problem.
My problem is that those exact things do not work.
The jbutton doesn't change its background color, a jlabel doesn't change it's text and so on.
These problems occur due to the repaint mechanizem which queue repaint requests instead of forcing them to happen. The process I'm doing in my jbutton's event (SQL query) doesn't seem to release enough resources to do those repaint actions.

Guy.
0
 
LVL 5

Accepted Solution

by:
fontaine earned 800 total points
ID: 1224841
Your problem is actually what you have mentioned in your comment: you are
doing the SQL query from within an AWT event handler. The  problem is that
while you're in an event handler no other events can get processed. The
solution is to do the SQL stuff in a separate thread and use the Observer/
Observable pattern to do the notification. Here is an example:

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Test extends Frame implements ActionListener, Observer {

    private Button button = null;


    public Test() {
        button = new Button("Do it!");
        button.setBackground(Color.gray);
        button.addActionListener(this);
        add(button);
    }

    public void actionPerformed(ActionEvent e) {
        button.setBackground(Color.red);
        button.setLabel("Please wait...");
       
        SQLRunner runner = new SQLRunner();
        runner.addObserver(this); // Test observes the SQLRunner...
        return;
    }

    // called by runner when the computing is finished
    public void update(Observable o, Object arg) {
        button.setBackground(Color.gray);
        button.setLabel("Do it!");
        return;
    }

    private class SQLRunner extends Observable implements Runnable {

        public SQLRunner() {
            Thread thread = new Thread(this);
            thread.start();
        }

        public void run() {
        // the operation takes a long time (here 5 sec)...

            try{
                Thread.currentThread().sleep(5000);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }

            // here the work is done -> we notify the observers
            setChanged();
            notifyObservers();

            return;
        }
    }

    public static void main(String args[]) {
        Test t = new Test();
        t.pack();
        t.show();
        return;
    }
}
0
 

Author Comment

by:optimal
ID: 1224842
As usual with your answers, it works great.

Thanks again,
Guy.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

610 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