How to display hourglass until request is finished

I want to display an hourglass (or alike) when a user clicks a button and has to wait several seconds until request is finished.How can I do this? Where can i find some simple examples?Where can I find other 'objects' to be used instead of an hourglass?
[283 byte] By [pgoovaertsa] at [2007-11-27 9:52:25]
# 1

package wait;

/*

* WaitCursorDemo.java

*/

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

import javax.swing.SwingWorker;

import test.*;

public class WaitCursorDemo extends JFrame {

private SwingWorker worker ;

private JButton btStart;

private JLabel lb;

public WaitCursorDemo() {

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setSize(400,300);

setLocationRelativeTo(null);

btStart = new JButton("Start");

add(btStart, BorderLayout.NORTH);

lb = new JLabel();

add(lb);

btStart.addActionListener(new ActionListener() {

public void actionPerformed(final ActionEvent e) {

start();

}

});

}

private void start(){

worker = new SwingWorker() {

protected Object doInBackground() throws Exception {

setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

lb.setText("Please wait...");

try{Thread.sleep(3000);}catch(InterruptedException ex){}

setCursor(Cursor.getDefaultCursor());

lb.setText("Done");

return null;

}

};

worker.execute();

}

public static void main(final String args[]) {

EventQueue.invokeLater(new Runnable() {

public void run() {

new WaitCursorDemo().setVisible(true);

}

});

}

/*

If you don't have JDK6, you need to download and compile the "SwingWorker" source code

to be able to use it. Here it is:

http://java.sun.com/products/jfc/tsc/articles/threads/src/SwingWorker.java

*

The API is a little different:

*

worker = new SwingWorker() {

public Object construct() {

setCursor(new Cursor(Cursor.WAIT_CURSOR));

lb.setText("Please wait...");

try{Thread.sleep(3000);}catch(InterruptedException ex){}

setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

lb.setText("Done");

return null;

}

};

worker.start();

*/

}

Message was edited by:

Andre_Uhres

Message was edited by:

Andre_Uhres

Andre_Uhresa at 2007-7-13 0:21:29 > top of Java-index,Desktop,Core GUI APIs...